Allow safe x/y = x * (1.0/y) for exact 1.0/y even if floating point constant folding...
[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                         int rem;
2808
2809                         /*
2810                          * Floating point constant folding might be disabled here to
2811                          * prevent rounding.
2812                          * However, as we check for exact result, doing it is safe.
2813                          * Switch it on.
2814                          */
2815                         rem = tarval_enable_fp_ops(1);
2816                         tv = tarval_quo(get_mode_one(mode), tv);
2817                         (void)tarval_enable_fp_ops(rem);
2818
2819                         /* Do the transformation if the result is either exact or we are not
2820                            using strict rules. */
2821                         if (tv != tarval_bad &&
2822                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2823                                 ir_node *blk = get_irn_n(n, -1);
2824                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2825                                 ir_node *a = get_Quot_left(n);
2826                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2827                                 ir_node *mem = get_Quot_mem(n);
2828
2829                                 /* skip a potential Pin */
2830                                 if (is_Pin(mem))
2831                                         mem = get_Pin_op(mem);
2832                                 turn_into_tuple(n, pn_Quot_max);
2833                                 set_Tuple_pred(n, pn_Quot_M, mem);
2834                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2835                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
2836                                 set_Tuple_pred(n, pn_Quot_res, m);
2837                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2838                         }
2839                 }
2840         }
2841         return n;
2842 }  /* transform_node_Quot */
2843
2844 /**
2845  * Optimize Abs(x) into  x if x is Confirmed >= 0
2846  * Optimize Abs(x) into -x if x is Confirmed <= 0
2847  * Optimize Abs(-x) int Abs(x)
2848  */
2849 static ir_node *transform_node_Abs(ir_node *n) {
2850         ir_node *c, *oldn = n;
2851         ir_node *a = get_Abs_op(n);
2852         ir_mode *mode;
2853
2854         HANDLE_UNOP_PHI(tarval_abs, a, c);
2855
2856         switch (classify_value_sign(a)) {
2857         case value_classified_negative:
2858                 mode = get_irn_mode(n);
2859
2860                 /*
2861                  * We can replace the Abs by -x here.
2862                  * We even could add a new Confirm here
2863                  * (if not twos complement)
2864                  *
2865                  * Note that -x would create a new node, so we could
2866                  * not run it in the equivalent_node() context.
2867                  */
2868                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2869                                 get_nodes_block(n), a, mode);
2870
2871                 DBG_OPT_CONFIRM(oldn, n);
2872                 return n;
2873         case value_classified_positive:
2874                 /* n is positive, Abs is not needed */
2875                 n = a;
2876
2877                 DBG_OPT_CONFIRM(oldn, n);
2878                 return n;
2879         default:
2880                 break;
2881         }
2882         if (is_Minus(a)) {
2883                 /* Abs(-x) = Abs(x) */
2884                 mode = get_irn_mode(n);
2885                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2886                                 get_nodes_block(n), get_Minus_op(a), mode);
2887                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2888                 return n;
2889         }
2890         return n;
2891 }  /* transform_node_Abs */
2892
2893 /**
2894  * Transform a Cond node.
2895  *
2896  * Replace the Cond by a Jmp if it branches on a constant
2897  * condition.
2898  */
2899 static ir_node *transform_node_Cond(ir_node *n) {
2900
2901         ir_node *jmp;
2902         ir_node *a = get_Cond_selector(n);
2903         tarval *ta = value_of(a);
2904
2905         /* we need block info which is not available in floating irgs */
2906         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2907                 return n;
2908
2909         if ((ta != tarval_bad) &&
2910             (get_irn_mode(a) == mode_b) &&
2911             (get_opt_unreachable_code())) {
2912                 /* It's a boolean Cond, branching on a boolean constant.
2913                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2914                 ir_node *blk = get_nodes_block(n);
2915                 jmp = new_r_Jmp(current_ir_graph, blk);
2916                 turn_into_tuple(n, pn_Cond_max);
2917                 if (ta == tarval_b_true) {
2918                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2919                         set_Tuple_pred(n, pn_Cond_true, jmp);
2920                 } else {
2921                         set_Tuple_pred(n, pn_Cond_false, jmp);
2922                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2923                 }
2924                 /* We might generate an endless loop, so keep it alive. */
2925                 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2926         }
2927         return n;
2928 }  /* transform_node_Cond */
2929
2930 /**
2931  * Prototype of a recursive transform function
2932  * for bitwise distributive transformations.
2933  */
2934 typedef ir_node* (*recursive_transform)(ir_node *n);
2935
2936 /**
2937  * makes use of distributive laws for and, or, eor
2938  *     and(a OP c, b OP c) -> and(a, b) OP c
2939  * note, might return a different op than n
2940  */
2941 static ir_node *transform_bitwise_distributive(ir_node *n,
2942                                                recursive_transform trans_func)
2943 {
2944         ir_node *oldn    = n;
2945         ir_node *a       = get_binop_left(n);
2946         ir_node *b       = get_binop_right(n);
2947         ir_op   *op      = get_irn_op(a);
2948         ir_op   *op_root = get_irn_op(n);
2949
2950         if(op != get_irn_op(b))
2951                 return n;
2952
2953         if (op == op_Conv) {
2954                 ir_node *a_op   = get_Conv_op(a);
2955                 ir_node *b_op   = get_Conv_op(b);
2956                 ir_mode *a_mode = get_irn_mode(a_op);
2957                 ir_mode *b_mode = get_irn_mode(b_op);
2958                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2959                         ir_node *blk = get_irn_n(n, -1);
2960
2961                         n = exact_copy(n);
2962                         set_binop_left(n, a_op);
2963                         set_binop_right(n, b_op);
2964                         set_irn_mode(n, a_mode);
2965                         n = trans_func(n);
2966                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2967
2968                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2969                         return n;
2970                 }
2971         }
2972
2973         if (op == op_Eor) {
2974                 /* nothing to gain here */
2975                 return n;
2976         }
2977
2978         if (op == op_Shrs || op == op_Shr || op == op_Shl
2979                         || op == op_And || op == op_Or || op == op_Eor) {
2980                 ir_node *a_left  = get_binop_left(a);
2981                 ir_node *a_right = get_binop_right(a);
2982                 ir_node *b_left  = get_binop_left(b);
2983                 ir_node *b_right = get_binop_right(b);
2984                 ir_node *c       = NULL;
2985                 ir_node *op1     = NULL;
2986                 ir_node *op2     = NULL;
2987
2988                 if (is_op_commutative(op)) {
2989                         if (a_left == b_left) {
2990                                 c   = a_left;
2991                                 op1 = a_right;
2992                                 op2 = b_right;
2993                         } else if(a_left == b_right) {
2994                                 c   = a_left;
2995                                 op1 = a_right;
2996                                 op2 = b_left;
2997                         } else if(a_right == b_left) {
2998                                 c   = a_right;
2999                                 op1 = a_left;
3000                                 op2 = b_right;
3001                         }
3002                 }
3003                 if(a_right == b_right) {
3004                         c   = a_right;
3005                         op1 = a_left;
3006                         op2 = b_left;
3007                 }
3008
3009                 if (c != NULL) {
3010                         /* (a sop c) & (b sop c) => (a & b) sop c */
3011                         ir_node *blk = get_irn_n(n, -1);
3012
3013                         ir_node *new_n = exact_copy(n);
3014                         set_binop_left(new_n, op1);
3015                         set_binop_right(new_n, op2);
3016                         new_n = trans_func(new_n);
3017
3018                         if(op_root == op_Eor && op == op_Or) {
3019                                 dbg_info  *dbgi = get_irn_dbg_info(n);
3020                                 ir_graph  *irg  = current_ir_graph;
3021                                 ir_mode   *mode = get_irn_mode(c);
3022
3023                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
3024                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3025                         } else {
3026                                 n = exact_copy(a);
3027                                 set_nodes_block(n, blk);
3028                                 set_binop_left(n, new_n);
3029                                 set_binop_right(n, c);
3030                                 add_identities(current_ir_graph->value_table, n);
3031                         }
3032
3033                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3034                         return n;
3035                 }
3036         }
3037
3038         return n;
3039 }
3040
3041 /**
3042  * Transform an And.
3043  */
3044 static ir_node *transform_node_And(ir_node *n) {
3045         ir_node *c, *oldn = n;
3046         ir_node *a = get_And_left(n);
3047         ir_node *b = get_And_right(n);
3048         ir_mode *mode;
3049
3050         mode = get_irn_mode(n);
3051         HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3052
3053         /* we can evaluate 2 Projs of the same Cmp */
3054         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3055                 ir_node *pred_a = get_Proj_pred(a);
3056                 ir_node *pred_b = get_Proj_pred(b);
3057                 if (pred_a == pred_b) {
3058                         dbg_info *dbgi  = get_irn_dbg_info(n);
3059                         ir_node  *block = get_nodes_block(pred_a);
3060                         pn_Cmp pn_a     = get_Proj_proj(a);
3061                         pn_Cmp pn_b     = get_Proj_proj(b);
3062                         /* yes, we can simply calculate with pncs */
3063                         pn_Cmp new_pnc  = pn_a & pn_b;
3064
3065                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3066                 }
3067         }
3068         if (is_Or(a)) {
3069                 if (is_Not(b)) {
3070                         ir_node *op = get_Not_op(b);
3071                         if (is_And(op)) {
3072                                 ir_node *ba = get_And_left(op);
3073                                 ir_node *bb = get_And_right(op);
3074
3075                                 /* it's enough to test the following cases due to normalization! */
3076                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3077                                         /* (a|b) & ~(a&b) = a^b */
3078                                         ir_node *block = get_nodes_block(n);
3079
3080                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3081                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3082                                         return n;
3083                                 }
3084                         }
3085                 }
3086         }
3087         if (is_Or(b)) {
3088                 if (is_Not(a)) {
3089                         ir_node *op = get_Not_op(a);
3090                         if (is_And(op)) {
3091                                 ir_node *aa = get_And_left(op);
3092                                 ir_node *ab = get_And_right(op);
3093
3094                                 /* it's enough to test the following cases due to normalization! */
3095                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3096                                         /* (a|b) & ~(a&b) = a^b */
3097                                         ir_node *block = get_nodes_block(n);
3098
3099                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3100                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3101                                         return n;
3102                                 }
3103                         }
3104                 }
3105         }
3106         if (is_Eor(a)) {
3107                 ir_node *al = get_Eor_left(a);
3108                 ir_node *ar = get_Eor_right(a);
3109
3110                 if (al == b) {
3111                         /* (b ^ a) & b -> ~a & b */
3112                         dbg_info *dbg  = get_irn_dbg_info(n);
3113                         ir_node *block = get_nodes_block(n);
3114
3115                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3116                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3117                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3118                         return n;
3119                 }
3120                 if (ar == b) {
3121                         /* (a ^ b) & b -> ~a & b */
3122                         dbg_info *dbg  = get_irn_dbg_info(n);
3123                         ir_node *block = get_nodes_block(n);
3124
3125                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3126                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3127                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3128                         return n;
3129                 }
3130         }
3131         if (is_Eor(b)) {
3132                 ir_node *bl = get_Eor_left(b);
3133                 ir_node *br = get_Eor_right(b);
3134
3135                 if (bl == a) {
3136                         /* a & (a ^ b) -> a & ~b */
3137                         dbg_info *dbg  = get_irn_dbg_info(n);
3138                         ir_node *block = get_nodes_block(n);
3139
3140                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3141                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3142                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3143                         return n;
3144                 }
3145                 if (br == a) {
3146                         /* a & (b ^ a) -> a & ~b */
3147                         dbg_info *dbg  = get_irn_dbg_info(n);
3148                         ir_node *block = get_nodes_block(n);
3149
3150                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3151                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3152                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3153                         return n;
3154                 }
3155         }
3156         if (is_Not(a) && is_Not(b)) {
3157                 /* ~a & ~b = ~(a|b) */
3158                 ir_node *block = get_nodes_block(n);
3159                 ir_mode *mode = get_irn_mode(n);
3160
3161                 a = get_Not_op(a);
3162                 b = get_Not_op(b);
3163                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3164                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3165                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3166                 return n;
3167         }
3168
3169         n = transform_bitwise_distributive(n, transform_node_And);
3170
3171         return n;
3172 }  /* transform_node_And */
3173
3174 /**
3175  * Transform an Eor.
3176  */
3177 static ir_node *transform_node_Eor(ir_node *n) {
3178         ir_node *c, *oldn = n;
3179         ir_node *a = get_Eor_left(n);
3180         ir_node *b = get_Eor_right(n);
3181         ir_mode *mode = get_irn_mode(n);
3182
3183         HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3184
3185         /* we can evaluate 2 Projs of the same Cmp */
3186         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3187                 ir_node *pred_a = get_Proj_pred(a);
3188                 ir_node *pred_b = get_Proj_pred(b);
3189                 if(pred_a == pred_b) {
3190                         dbg_info *dbgi  = get_irn_dbg_info(n);
3191                         ir_node  *block = get_nodes_block(pred_a);
3192                         pn_Cmp pn_a     = get_Proj_proj(a);
3193                         pn_Cmp pn_b     = get_Proj_proj(b);
3194                         /* yes, we can simply calculate with pncs */
3195                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3196
3197                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3198                                            new_pnc);
3199                 }
3200         }
3201
3202         if (a == b) {
3203                 /* a ^ a = 0 */
3204                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3205                                  mode, get_mode_null(mode));
3206                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3207         } else if (mode == mode_b &&
3208                         is_Proj(a) &&
3209                         is_Const(b) && is_Const_one(b) &&
3210                         is_Cmp(get_Proj_pred(a))) {
3211                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3212                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3213                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3214
3215                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3216         } else if (is_Const(b)) {
3217                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3218                         ir_node  *cnst   = new_Const(mode, tarval_not(get_Const_tarval(b)));
3219                         ir_node  *not_op = get_Not_op(a);
3220                         dbg_info *dbg    = get_irn_dbg_info(n);
3221                         ir_graph *irg    = current_ir_graph;
3222                         ir_node  *block  = get_nodes_block(n);
3223                         ir_mode  *mode   = get_irn_mode(n);
3224                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3225                         return n;
3226                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3227                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3228                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3229                 }
3230         } else {
3231                 n = transform_bitwise_distributive(n, transform_node_Eor);
3232         }
3233
3234         return n;
3235 }  /* transform_node_Eor */
3236
3237 /**
3238  * Transform a Not.
3239  */
3240 static ir_node *transform_node_Not(ir_node *n) {
3241         ir_node *c, *oldn = n;
3242         ir_node *a    = get_Not_op(n);
3243         ir_mode *mode = get_irn_mode(n);
3244
3245         HANDLE_UNOP_PHI(tarval_not,a,c);
3246
3247         /* check for a boolean Not */
3248         if (mode == mode_b &&
3249                         is_Proj(a) &&
3250                         is_Cmp(get_Proj_pred(a))) {
3251                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3252                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3253                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3254                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3255                 return n;
3256         }
3257         if  (is_Eor(a)) {
3258                 ir_node *eor_b = get_Eor_right(a);
3259                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3260                         ir_node  *cnst  = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3261                         ir_node  *eor_a = get_Eor_left(a);
3262                         dbg_info *dbg   = get_irn_dbg_info(n);
3263                         ir_graph *irg   = current_ir_graph;
3264                         ir_node  *block = get_nodes_block(n);
3265                         ir_mode  *mode  = get_irn_mode(n);
3266                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3267                         return n;
3268                 }
3269         }
3270         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3271                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3272                         dbg_info *dbg   = get_irn_dbg_info(n);
3273                         ir_graph *irg   = current_ir_graph;
3274                         ir_node  *block = get_nodes_block(n);
3275                         ir_node  *add_l = get_Minus_op(a);
3276                         ir_node  *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3277                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3278                 } else if (is_Add(a)) {
3279                         ir_node *add_r = get_Add_right(a);
3280                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3281                                 /* ~(x + -1) = -x */
3282                                 ir_node *op = get_Add_left(a);
3283                                 ir_node *blk = get_irn_n(n, -1);
3284                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3285                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3286                         }
3287                 }
3288         }
3289         return n;
3290 }  /* transform_node_Not */
3291
3292 /**
3293  * Transform a Minus.
3294  * Optimize:
3295  *   -(~x) = x + 1
3296  *   -(a-b) = b - a
3297  *   -(a >>u (size-1)) = a >>s (size-1)
3298  *   -(a >>s (size-1)) = a >>u (size-1)
3299  *   -(a * const) -> a * -const
3300  */
3301 static ir_node *transform_node_Minus(ir_node *n) {
3302         ir_node *c, *oldn = n;
3303         ir_node *a = get_Minus_op(n);
3304         ir_mode *mode;
3305
3306         HANDLE_UNOP_PHI(tarval_neg,a,c);
3307
3308         mode = get_irn_mode(a);
3309         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3310                 /* the following rules are only to twos-complement */
3311                 if (is_Not(a)) {
3312                         /* -(~x) = x + 1 */
3313                         ir_node *op   = get_Not_op(a);
3314                         tarval *tv    = get_mode_one(mode);
3315                         ir_node *blk  = get_irn_n(n, -1);
3316                         ir_node *c    = new_r_Const(current_ir_graph, blk, mode, tv);
3317                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3318                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3319                         return n;
3320                 }
3321                 if (is_Shr(a)) {
3322                         ir_node *c = get_Shr_right(a);
3323
3324                         if (is_Const(c)) {
3325                                 tarval *tv = get_Const_tarval(c);
3326
3327                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3328                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3329                                         ir_node *v = get_Shr_left(a);
3330
3331                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3332                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3333                                         return n;
3334                                 }
3335                         }
3336                 }
3337                 if (is_Shrs(a)) {
3338                         ir_node *c = get_Shrs_right(a);
3339
3340                         if (is_Const(c)) {
3341                                 tarval *tv = get_Const_tarval(c);
3342
3343                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3344                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3345                                         ir_node *v = get_Shrs_left(a);
3346
3347                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3348                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3349                                         return n;
3350                                 }
3351                         }
3352                 }
3353         }
3354         if (is_Sub(a)) {
3355                 /* - (a-b) = b - a */
3356                 ir_node *la  = get_Sub_left(a);
3357                 ir_node *ra  = get_Sub_right(a);
3358                 ir_node *blk = get_irn_n(n, -1);
3359
3360                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3361                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3362                 return n;
3363         }
3364
3365         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3366                 ir_node *mul_l = get_Mul_left(a);
3367                 ir_node *mul_r = get_Mul_right(a);
3368                 if (is_Const(mul_r)) {
3369                         tarval   *tv    = tarval_neg(get_Const_tarval(mul_r));
3370                         if(tv != tarval_bad) {
3371                                 ir_node  *cnst  = new_Const(mode, tv);
3372                                 dbg_info *dbg   = get_irn_dbg_info(a);
3373                                 ir_graph *irg   = current_ir_graph;
3374                                 ir_node  *block = get_nodes_block(a);
3375                                 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3376                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3377                                 return n;
3378                         }
3379                 }
3380         }
3381
3382         return n;
3383 }  /* transform_node_Minus */
3384
3385 /**
3386  * Transform a Cast_type(Const) into a new Const_type
3387  */
3388 static ir_node *transform_node_Cast(ir_node *n) {
3389         ir_node *oldn = n;
3390         ir_node *pred = get_Cast_op(n);
3391         ir_type *tp = get_irn_type(n);
3392
3393         if (is_Const(pred) && get_Const_type(pred) != tp) {
3394                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3395                         get_Const_tarval(pred), tp);
3396                 DBG_OPT_CSTEVAL(oldn, n);
3397         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3398                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3399                         get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3400                 DBG_OPT_CSTEVAL(oldn, n);
3401         }
3402
3403         return n;
3404 }  /* transform_node_Cast */
3405
3406 /**
3407  * Transform a Proj(Div) with a non-zero value.
3408  * Removes the exceptions and routes the memory to the NoMem node.
3409  */
3410 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3411         ir_node *div = get_Proj_pred(proj);
3412         ir_node *b   = get_Div_right(div);
3413         ir_node *confirm, *res, *new_mem;
3414         long proj_nr;
3415
3416         if (value_not_zero(b, &confirm)) {
3417                 /* div(x, y) && y != 0 */
3418                 if (confirm == NULL) {
3419                         /* we are sure we have a Const != 0 */
3420                         new_mem = get_Div_mem(div);
3421                         if (is_Pin(new_mem))
3422                                 new_mem = get_Pin_op(new_mem);
3423                         set_Div_mem(div, new_mem);
3424                         set_irn_pinned(div, op_pin_state_floats);
3425                 }
3426
3427                 proj_nr = get_Proj_proj(proj);
3428                 switch (proj_nr) {
3429                 case pn_Div_X_regular:
3430                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3431
3432                 case pn_Div_X_except:
3433                         /* we found an exception handler, remove it */
3434                         DBG_OPT_EXC_REM(proj);
3435                         return new_Bad();
3436
3437                 case pn_Div_M:
3438                         res = get_Div_mem(div);
3439                         new_mem = get_irg_no_mem(current_ir_graph);
3440
3441                         if (confirm) {
3442                                 /* This node can only float up to the Confirm block */
3443                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3444                         }
3445                         set_irn_pinned(div, op_pin_state_floats);
3446                         /* this is a Div without exception, we can remove the memory edge */
3447                         set_Div_mem(div, new_mem);
3448                         return res;
3449                 }
3450         }
3451         return proj;
3452 }  /* transform_node_Proj_Div */
3453
3454 /**
3455  * Transform a Proj(Mod) with a non-zero value.
3456  * Removes the exceptions and routes the memory to the NoMem node.
3457  */
3458 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3459         ir_node *mod = get_Proj_pred(proj);
3460         ir_node *b   = get_Mod_right(mod);
3461         ir_node *confirm, *res, *new_mem;
3462         long proj_nr;
3463
3464         if (value_not_zero(b, &confirm)) {
3465                 /* mod(x, y) && y != 0 */
3466                 proj_nr = get_Proj_proj(proj);
3467
3468                 if (confirm == NULL) {
3469                         /* we are sure we have a Const != 0 */
3470                         new_mem = get_Mod_mem(mod);
3471                         if (is_Pin(new_mem))
3472                                 new_mem = get_Pin_op(new_mem);
3473                         set_Mod_mem(mod, new_mem);
3474                         set_irn_pinned(mod, op_pin_state_floats);
3475                 }
3476
3477                 switch (proj_nr) {
3478
3479                 case pn_Mod_X_regular:
3480                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3481
3482                 case pn_Mod_X_except:
3483                         /* we found an exception handler, remove it */
3484                         DBG_OPT_EXC_REM(proj);
3485                         return new_Bad();
3486
3487                 case pn_Mod_M:
3488                         res = get_Mod_mem(mod);
3489                         new_mem = get_irg_no_mem(current_ir_graph);
3490
3491                         if (confirm) {
3492                                 /* This node can only float up to the Confirm block */
3493                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3494                         }
3495                         /* this is a Mod without exception, we can remove the memory edge */
3496                         set_Mod_mem(mod, new_mem);
3497                         return res;
3498                 case pn_Mod_res:
3499                         if (get_Mod_left(mod) == b) {
3500                                 /* a % a = 0 if a != 0 */
3501                                 ir_mode *mode = get_irn_mode(proj);
3502                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3503
3504                                 DBG_OPT_CSTEVAL(mod, res);
3505                                 return res;
3506                         }
3507                 }
3508         }
3509         return proj;
3510 }  /* transform_node_Proj_Mod */
3511
3512 /**
3513  * Transform a Proj(DivMod) with a non-zero value.
3514  * Removes the exceptions and routes the memory to the NoMem node.
3515  */
3516 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3517         ir_node *divmod = get_Proj_pred(proj);
3518         ir_node *b      = get_DivMod_right(divmod);
3519         ir_node *confirm, *res, *new_mem;
3520         long proj_nr;
3521
3522         if (value_not_zero(b, &confirm)) {
3523                 /* DivMod(x, y) && y != 0 */
3524                 proj_nr = get_Proj_proj(proj);
3525
3526                 if (confirm == NULL) {
3527                         /* we are sure we have a Const != 0 */
3528                         new_mem = get_DivMod_mem(divmod);
3529                         if (is_Pin(new_mem))
3530                                 new_mem = get_Pin_op(new_mem);
3531                         set_DivMod_mem(divmod, new_mem);
3532                         set_irn_pinned(divmod, op_pin_state_floats);
3533                 }
3534
3535                 switch (proj_nr) {
3536
3537                 case pn_DivMod_X_regular:
3538                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3539
3540                 case pn_DivMod_X_except:
3541                         /* we found an exception handler, remove it */
3542                         DBG_OPT_EXC_REM(proj);
3543                         return new_Bad();
3544
3545                 case pn_DivMod_M:
3546                         res = get_DivMod_mem(divmod);
3547                         new_mem = get_irg_no_mem(current_ir_graph);
3548
3549                         if (confirm) {
3550                                 /* This node can only float up to the Confirm block */
3551                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3552                         }
3553                         /* this is a DivMod without exception, we can remove the memory edge */
3554                         set_DivMod_mem(divmod, new_mem);
3555                         return res;
3556
3557                 case pn_DivMod_res_mod:
3558                         if (get_DivMod_left(divmod) == b) {
3559                                 /* a % a = 0 if a != 0 */
3560                                 ir_mode *mode = get_irn_mode(proj);
3561                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3562
3563                                 DBG_OPT_CSTEVAL(divmod, res);
3564                                 return res;
3565                         }
3566                 }
3567         }
3568         return proj;
3569 }  /* transform_node_Proj_DivMod */
3570
3571 /**
3572  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3573  */
3574 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3575         if (get_opt_unreachable_code()) {
3576                 ir_node *n = get_Proj_pred(proj);
3577                 ir_node *b = get_Cond_selector(n);
3578
3579                 if (mode_is_int(get_irn_mode(b))) {
3580                         tarval *tb = value_of(b);
3581
3582                         if (tb != tarval_bad) {
3583                                 /* we have a constant switch */
3584                                 long num = get_Proj_proj(proj);
3585
3586                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3587                                         if (get_tarval_long(tb) == num) {
3588                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3589                                                  * in a block. This case is optimized away in optimize_cf(). */
3590                                                 return proj;
3591                                         } else {
3592                                                 /* this case will NEVER be taken, kill it */
3593                                                 return new_Bad();
3594                                         }
3595                                 }
3596                         }
3597                 }
3598         }
3599         return proj;
3600 }  /* transform_node_Proj_Cond */
3601
3602 /**
3603  * Create a 0 constant of given mode.
3604  */
3605 static ir_node *create_zero_const(ir_mode *mode) {
3606         tarval   *tv    = get_mode_null(mode);
3607         ir_node  *cnst  = new_Const(mode, tv);
3608
3609         return cnst;
3610 }
3611
3612 /* the order of the values is important! */
3613 typedef enum const_class {
3614         const_const = 0,
3615         const_like  = 1,
3616         const_other = 2
3617 } const_class;
3618
3619 static const_class classify_const(const ir_node* n)
3620 {
3621         if (is_Const(n))         return const_const;
3622         if (is_irn_constlike(n)) return const_like;
3623         return const_other;
3624 }
3625
3626 /**
3627  * Determines whether r is more constlike or has a larger index (in that order)
3628  * than l.
3629  */
3630 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3631 {
3632         const const_class l_order = classify_const(l);
3633         const const_class r_order = classify_const(r);
3634         return
3635                 l_order > r_order ||
3636                 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3637 }
3638
3639 /**
3640  * Normalizes and optimizes Cmp nodes.
3641  */
3642 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3643         ir_node      *n      = get_Proj_pred(proj);
3644         ir_node      *left   = get_Cmp_left(n);
3645         ir_node      *right  = get_Cmp_right(n);
3646         tarval      *tv      = NULL;
3647         int          changed = 0;
3648         ir_mode     *mode    = NULL;
3649         long         proj_nr = get_Proj_proj(proj);
3650
3651         /* we can evaluate some cases directly */
3652         switch (proj_nr) {
3653         case pn_Cmp_False:
3654                 return new_Const(mode_b, get_tarval_b_false());
3655         case pn_Cmp_True:
3656                 return new_Const(mode_b, get_tarval_b_true());
3657         case pn_Cmp_Leg:
3658                 if (!mode_is_float(get_irn_mode(left)))
3659                         return new_Const(mode_b, get_tarval_b_true());
3660                 break;
3661         default:
3662                 break;
3663         }
3664
3665         /* remove Casts of both sides */
3666         left  = skip_Cast(left);
3667         right = skip_Cast(right);
3668
3669         /* Remove unnecessary conversions */
3670         /* TODO handle constants */
3671         if (is_Conv(left) && is_Conv(right)) {
3672                 ir_mode *mode        = get_irn_mode(left);
3673                 ir_node *op_left     = get_Conv_op(left);
3674                 ir_node *op_right    = get_Conv_op(right);
3675                 ir_mode *mode_left   = get_irn_mode(op_left);
3676                 ir_mode *mode_right  = get_irn_mode(op_right);
3677
3678                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3679                                 && mode_left != mode_b && mode_right != mode_b) {
3680                         ir_graph *irg   = current_ir_graph;
3681                         ir_node  *block = get_nodes_block(n);
3682
3683                         if (mode_left == mode_right) {
3684                                 left  = op_left;
3685                                 right = op_right;
3686                                 changed |= 1;
3687                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3688                         } else if (smaller_mode(mode_left, mode_right)) {
3689                                 left  = new_r_Conv(irg, block, op_left, mode_right);
3690                                 right = op_right;
3691                                 changed |= 1;
3692                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3693                         } else if (smaller_mode(mode_right, mode_left)) {
3694                                 left  = op_left;
3695                                 right = new_r_Conv(irg, block, op_right, mode_left);
3696                                 changed |= 1;
3697                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3698                         }
3699                 }
3700         }
3701
3702         /* remove operation on both sides if possible */
3703         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3704                 /*
3705                  * The following operations are NOT safe for floating point operations, for instance
3706                  * 1.0 + inf == 2.0 + inf, =/=> x == y
3707                  */
3708                 if (mode_is_int(get_irn_mode(left))) {
3709                         unsigned lop = get_irn_opcode(left);
3710
3711                         if (lop == get_irn_opcode(right)) {
3712                                 ir_node *ll, *lr, *rl, *rr;
3713
3714                                 /* same operation on both sides, try to remove */
3715                                 switch (lop) {
3716                                 case iro_Not:
3717                                 case iro_Minus:
3718                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3719                                         left  = get_unop_op(left);
3720                                         right = get_unop_op(right);
3721                                         changed |= 1;
3722                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3723                                         break;
3724                                 case iro_Add:
3725                                         ll = get_Add_left(left);
3726                                         lr = get_Add_right(left);
3727                                         rl = get_Add_left(right);
3728                                         rr = get_Add_right(right);
3729
3730                                         if (ll == rl) {
3731                                                 /* X + a CMP X + b ==> a CMP b */
3732                                                 left  = lr;
3733                                                 right = rr;
3734                                                 changed |= 1;
3735                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3736                                         } else if (ll == rr) {
3737                                                 /* X + a CMP b + X ==> a CMP b */
3738                                                 left  = lr;
3739                                                 right = rl;
3740                                                 changed |= 1;
3741                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3742                                         } else if (lr == rl) {
3743                                                 /* a + X CMP X + b ==> a CMP b */
3744                                                 left  = ll;
3745                                                 right = rr;
3746                                                 changed |= 1;
3747                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3748                                         } else if (lr == rr) {
3749                                                 /* a + X CMP b + X ==> a CMP b */
3750                                                 left  = ll;
3751                                                 right = rl;
3752                                                 changed |= 1;
3753                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3754                                         }
3755                                         break;
3756                                 case iro_Sub:
3757                                         ll = get_Sub_left(left);
3758                                         lr = get_Sub_right(left);
3759                                         rl = get_Sub_left(right);
3760                                         rr = get_Sub_right(right);
3761
3762                                         if (ll == rl) {
3763                                                 /* X - a CMP X - b ==> a CMP b */
3764                                                 left  = lr;
3765                                                 right = rr;
3766                                                 changed |= 1;
3767                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3768                                         } else if (lr == rr) {
3769                                                 /* a - X CMP b - X ==> a CMP b */
3770                                                 left  = ll;
3771                                                 right = rl;
3772                                                 changed |= 1;
3773                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3774                                         }
3775                                         break;
3776                                 case iro_Rot:
3777                                         if (get_Rot_right(left) == get_Rot_right(right)) {
3778                                                 /* a ROT X CMP b ROT X ==> a CMP b */
3779                                                 left  = get_Rot_left(left);
3780                                                 right = get_Rot_left(right);
3781                                                 changed |= 1;
3782                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3783                                         }
3784                                         break;
3785                                 default:
3786                                         break;
3787                                 }
3788                         }
3789
3790                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
3791                         if (is_Add(left) || is_Sub(left)) {
3792                                 ir_node *ll = get_binop_left(left);
3793                                 ir_node *lr = get_binop_right(left);
3794
3795                                 if (lr == right && is_Add(left)) {
3796                                         ir_node *tmp = ll;
3797                                         ll = lr;
3798                                         lr = tmp;
3799                                 }
3800                                 if (ll == right) {
3801                                         left     = lr;
3802                                         right    = create_zero_const(get_irn_mode(left));
3803                                         changed |= 1;
3804                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3805                                 }
3806                         }
3807                         if (is_Add(right) || is_Sub(right)) {
3808                                 ir_node *rl = get_binop_left(right);
3809                                 ir_node *rr = get_binop_right(right);
3810
3811                                 if (rr == left && is_Add(right)) {
3812                                         ir_node *tmp = rl;
3813                                         rl = rr;
3814                                         rr = tmp;
3815                                 }
3816                                 if (rl == left) {
3817                                         left     = rr;
3818                                         right    = create_zero_const(get_irn_mode(left));
3819                                         changed |= 1;
3820                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3821                                 }
3822                         }
3823                 }  /* mode_is_int(...) */
3824         }  /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3825
3826         /* replace mode_b compares with ands/ors */
3827         if (get_irn_mode(left) == mode_b) {
3828                 ir_graph *irg   = current_ir_graph;
3829                 ir_node  *block = get_nodes_block(n);
3830                 ir_node  *bres;
3831
3832                 switch (proj_nr) {
3833                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3834                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3835                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3836                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3837                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3838                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3839                         default: bres = NULL;
3840                 }
3841                 if (bres) {
3842                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3843                         return bres;
3844                 }
3845         }
3846
3847         /*
3848          * First step: normalize the compare op
3849          * by placing the constant on the right side
3850          * or moving the lower address node to the left.
3851          */
3852         if (!operands_are_normalized(left, right)) {
3853                 ir_node *t = left;
3854
3855                 left  = right;
3856                 right = t;
3857
3858                 proj_nr = get_inversed_pnc(proj_nr);
3859                 changed |= 1;
3860         }
3861
3862         /*
3863          * Second step: Try to reduce the magnitude
3864          * of a constant. This may help to generate better code
3865          * later and may help to normalize more compares.
3866          * Of course this is only possible for integer values.
3867          */
3868         if (is_Const(right)) {
3869                 mode = get_irn_mode(right);
3870                 tv = get_Const_tarval(right);
3871
3872                 /* TODO extend to arbitrary constants */
3873                 if (is_Conv(left) && tarval_is_null(tv)) {
3874                         ir_node *op      = get_Conv_op(left);
3875                         ir_mode *op_mode = get_irn_mode(op);
3876
3877                         /*
3878                          * UpConv(x) REL 0  ==> x REL 0
3879                          */
3880                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3881                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3882                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3883                                 tv   = get_mode_null(op_mode);
3884                                 left = op;
3885                                 mode = op_mode;
3886                                 changed |= 2;
3887                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3888                         }
3889                 }
3890
3891                 if (tv != tarval_bad) {
3892                         /* the following optimization is possible on modes without Overflow
3893                          * on Unary Minus or on == and !=:
3894                          * -a CMP c  ==>  a swap(CMP) -c
3895                          *
3896                          * Beware: for two-complement Overflow may occur, so only == and != can
3897                          * be optimized, see this:
3898                          * -MININT < 0 =/=> MININT > 0 !!!
3899                          */
3900                         if (is_Minus(left) &&
3901                                 (!mode_overflow_on_unary_Minus(mode) ||
3902                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3903                                 tv = tarval_neg(tv);
3904
3905                                 if (tv != tarval_bad) {
3906                                         left = get_Minus_op(left);
3907                                         proj_nr = get_inversed_pnc(proj_nr);
3908                                         changed |= 2;
3909                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3910                                 }
3911                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3912                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
3913                                 tv = tarval_not(tv);
3914
3915                                 if (tv != tarval_bad) {
3916                                         left = get_Not_op(left);
3917                                         changed |= 2;
3918                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3919                                 }
3920                         }
3921
3922                         /* for integer modes, we have more */
3923                         if (mode_is_int(mode)) {
3924                                 /* Ne includes Unordered which is not possible on integers.
3925                                  * However, frontends often use this wrong, so fix it here */
3926                                 if (proj_nr & pn_Cmp_Uo) {
3927                                         proj_nr &= ~pn_Cmp_Uo;
3928                                         set_Proj_proj(proj, proj_nr);
3929                                 }
3930
3931                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
3932                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3933                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3934                                         tv = tarval_sub(tv, get_mode_one(mode));
3935
3936                                         if (tv != tarval_bad) {
3937                                                 proj_nr ^= pn_Cmp_Eq;
3938                                                 changed |= 2;
3939                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3940                                         }
3941                                 }
3942                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
3943                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3944                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3945                                         tv = tarval_add(tv, get_mode_one(mode));
3946
3947                                         if (tv != tarval_bad) {
3948                                                 proj_nr ^= pn_Cmp_Eq;
3949                                                 changed |= 2;
3950                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3951                                         }
3952                                 }
3953
3954                                 /* the following reassociations work only for == and != */
3955                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3956
3957 #if 0 /* Might be not that good in general */
3958                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
3959                                         if (tarval_is_null(tv) && is_Sub(left)) {
3960                                                 right = get_Sub_right(left);
3961                                                 left  = get_Sub_left(left);
3962
3963                                                 tv = value_of(right);
3964                                                 changed = 1;
3965                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3966                                         }
3967 #endif
3968
3969                                         if (tv != tarval_bad) {
3970                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
3971                                                 if (is_Sub(left)) {
3972                                                         ir_node *c1 = get_Sub_right(left);
3973                                                         tarval *tv2 = value_of(c1);
3974
3975                                                         if (tv2 != tarval_bad) {
3976                                                                 tv2 = tarval_add(tv, value_of(c1));
3977
3978                                                                 if (tv2 != tarval_bad) {
3979                                                                         left    = get_Sub_left(left);
3980                                                                         tv      = tv2;
3981                                                                         changed |= 2;
3982                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3983                                                                 }
3984                                                         }
3985                                                 }
3986                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
3987                                                 else if (is_Add(left)) {
3988                                                         ir_node *a_l = get_Add_left(left);
3989                                                         ir_node *a_r = get_Add_right(left);
3990                                                         ir_node *a;
3991                                                         tarval *tv2;
3992
3993                                                         if (is_Const(a_l)) {
3994                                                                 a = a_r;
3995                                                                 tv2 = value_of(a_l);
3996                                                         } else {
3997                                                                 a = a_l;
3998                                                                 tv2 = value_of(a_r);
3999                                                         }
4000
4001                                                         if (tv2 != tarval_bad) {
4002                                                                 tv2 = tarval_sub(tv, tv2);
4003
4004                                                                 if (tv2 != tarval_bad) {
4005                                                                         left    = a;
4006                                                                         tv      = tv2;
4007                                                                         changed |= 2;
4008                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4009                                                                 }
4010                                                         }
4011                                                 }
4012                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4013                                                 else if (is_Minus(left)) {
4014                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4015
4016                                                         if (tv2 != tarval_bad) {
4017                                                                 left    = get_Minus_op(left);
4018                                                                 tv      = tv2;
4019                                                                 changed |= 2;
4020                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4021                                                         }
4022                                                 }
4023                                         }
4024                                 } /* == or != */
4025                                 /* the following reassociations work only for <= */
4026                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4027                                         if (tv != tarval_bad) {
4028                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
4029                                                 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4030                                                 }
4031                                         }
4032                                 }
4033                         } /* mode_is_int */
4034
4035                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4036                                 switch (get_irn_opcode(left)) {
4037                                         ir_node *c1;
4038
4039                                 case iro_And:
4040                                         c1 = get_And_right(left);
4041                                         if (is_Const(c1)) {
4042                                                 /*
4043                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4044                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4045                                                  */
4046                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4047                                                 if (mask != tv) {
4048                                                         /* TODO: move to constant evaluation */
4049                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4050                                                         c1 = new_Const(mode_b, tv);
4051                                                         DBG_OPT_CSTEVAL(proj, c1);
4052                                                         return c1;
4053                                                 }
4054
4055                                                 if (tarval_is_single_bit(tv)) {
4056                                                         /*
4057                                                          * optimization for AND:
4058                                                          * Optimize:
4059                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4060                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4061                                                          *
4062                                                          * if C is a single Bit constant.
4063                                                          */
4064
4065                                                         /* check for Constant's match. We have check hare the tarvals,
4066                                                            because our const might be changed */
4067                                                         if (get_Const_tarval(c1) == tv) {
4068                                                                 /* fine: do the transformation */
4069                                                                 tv = get_mode_null(get_tarval_mode(tv));
4070                                                                 proj_nr ^= pn_Cmp_Leg;
4071                                                                 changed |= 2;
4072                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4073                                                         }
4074                                                 }
4075                                         }
4076                                         break;
4077                                 case iro_Or:
4078                                         c1 = get_Or_right(left);
4079                                         if (is_Const(c1) && tarval_is_null(tv)) {
4080                                                 /*
4081                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4082                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4083                                                  */
4084                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4085                                                         /* TODO: move to constant evaluation */
4086                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4087                                                         c1 = new_Const(mode_b, tv);
4088                                                         DBG_OPT_CSTEVAL(proj, c1);
4089                                                         return c1;
4090                                                 }
4091                                         }
4092                                         break;
4093                                 case iro_Shl:
4094                                         /*
4095                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4096                                          *                             FALSE                       else
4097                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4098                                          *                             TRUE                        else
4099                                          */
4100                                         c1 = get_Shl_right(left);
4101                                         if (is_Const(c1)) {
4102                                                 tarval  *tv1    = get_Const_tarval(c1);
4103                                                 ir_mode *mode   = get_irn_mode(left);
4104                                                 tarval  *minus1 = get_mode_all_one(mode);
4105                                                 tarval  *amask  = tarval_shr(minus1, tv1);
4106                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
4107                                                 ir_node *sl, *blk;
4108
4109                                                 if (tarval_and(tv, cmask) != tv) {
4110                                                         /* condition not met */
4111                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4112                                                         c1 = new_Const(mode_b, tv);
4113                                                         DBG_OPT_CSTEVAL(proj, c1);
4114                                                         return c1;
4115                                                 }
4116                                                 sl   = get_Shl_left(left);
4117                                                 blk  = get_nodes_block(n);
4118                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4119                                                 tv   = tarval_shr(tv, tv1);
4120                                                 changed |= 2;
4121                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4122                                         }
4123                                         break;
4124                                 case iro_Shr:
4125                                         /*
4126                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4127                                          *                             FALSE                       else
4128                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4129                                          *                             TRUE                        else
4130                                          */
4131                                         c1 = get_Shr_right(left);
4132                                         if (is_Const(c1)) {
4133                                                 tarval  *tv1    = get_Const_tarval(c1);
4134                                                 ir_mode *mode   = get_irn_mode(left);
4135                                                 tarval  *minus1 = get_mode_all_one(mode);
4136                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4137                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
4138                                                 ir_node *sl, *blk;
4139
4140                                                 if (tarval_and(tv, cmask) != tv) {
4141                                                         /* condition not met */
4142                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4143                                                         c1 = new_Const(mode_b, tv);
4144                                                         DBG_OPT_CSTEVAL(proj, c1);
4145                                                         return c1;
4146                                                 }
4147                                                 sl   = get_Shr_left(left);
4148                                                 blk  = get_nodes_block(n);
4149                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4150                                                 tv   = tarval_shl(tv, tv1);
4151                                                 changed |= 2;
4152                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4153                                         }
4154                                         break;
4155                                 case iro_Shrs:
4156                                         /*
4157                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4158                                          *                             FALSE                       else
4159                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4160                                          *                             TRUE                        else
4161                                          */
4162                                         c1 = get_Shrs_right(left);
4163                                         if (is_Const(c1)) {
4164                                                 tarval  *tv1    = get_Const_tarval(c1);
4165                                                 ir_mode *mode   = get_irn_mode(left);
4166                                                 tarval  *minus1 = get_mode_all_one(mode);
4167                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4168                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4169                                                 ir_node *sl, *blk;
4170
4171                                                 cond = tarval_sub(cond, tv1);
4172                                                 cond = tarval_shrs(tv, cond);
4173
4174                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4175                                                         /* condition not met */
4176                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4177                                                         c1 = new_Const(mode_b, tv);
4178                                                         DBG_OPT_CSTEVAL(proj, c1);
4179                                                         return c1;
4180                                                 }
4181                                                 sl   = get_Shrs_left(left);
4182                                                 blk  = get_nodes_block(n);
4183                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4184                                                 tv   = tarval_shl(tv, tv1);
4185                                                 changed |= 2;
4186                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4187                                         }
4188                                         break;
4189                                 }  /* switch */
4190                         }
4191                 } /* tarval != bad */
4192         }
4193
4194         if (changed & 2)      /* need a new Const */
4195                 right = new_Const(mode, tv);
4196
4197         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4198                 ir_node *op = get_Proj_pred(left);
4199
4200                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4201                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4202                         ir_node *c = get_binop_right(op);
4203
4204                         if (is_Const(c)) {
4205                                 tarval *tv = get_Const_tarval(c);
4206
4207                                 if (tarval_is_single_bit(tv)) {
4208                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4209                                         ir_node *v    = get_binop_left(op);
4210                                         ir_node *blk  = get_irn_n(op, -1);
4211                                         ir_mode *mode = get_irn_mode(v);
4212
4213                                         tv = tarval_sub(tv, get_mode_one(mode));
4214                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4215                                         changed |= 1;
4216                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4217                                 }
4218                         }
4219                 }
4220         }
4221
4222         if (changed) {
4223                 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4224
4225                 /* create a new compare */
4226                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4227                 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4228         }
4229
4230         return proj;
4231 }  /* transform_node_Proj_Cmp */
4232
4233 /**
4234  * Does all optimizations on nodes that must be done on it's Proj's
4235  * because of creating new nodes.
4236  */
4237 static ir_node *transform_node_Proj(ir_node *proj) {
4238         ir_node *n = get_Proj_pred(proj);
4239
4240         switch (get_irn_opcode(n)) {
4241         case iro_Div:
4242                 return transform_node_Proj_Div(proj);
4243
4244         case iro_Mod:
4245                 return transform_node_Proj_Mod(proj);
4246
4247         case iro_DivMod:
4248                 return transform_node_Proj_DivMod(proj);
4249
4250         case iro_Cond:
4251                 return transform_node_Proj_Cond(proj);
4252
4253         case iro_Cmp:
4254                 return transform_node_Proj_Cmp(proj);
4255
4256         case iro_Tuple:
4257                 /* should not happen, but if it does will be optimized away */
4258                 return equivalent_node_Proj(proj);
4259
4260         default:
4261                 /* do nothing */
4262                 return proj;
4263         }
4264 }  /* transform_node_Proj */
4265
4266 /**
4267  * Move Confirms down through Phi nodes.
4268  */
4269 static ir_node *transform_node_Phi(ir_node *phi) {
4270         int i, n;
4271         ir_mode *mode = get_irn_mode(phi);
4272
4273         if (mode_is_reference(mode)) {
4274                 n = get_irn_arity(phi);
4275
4276                 /* Beware of Phi0 */
4277                 if (n > 0) {
4278                         ir_node *pred = get_irn_n(phi, 0);
4279                         ir_node *bound, *new_Phi, *block, **in;
4280                         pn_Cmp  pnc;
4281
4282                         if (! is_Confirm(pred))
4283                                 return phi;
4284
4285                         bound = get_Confirm_bound(pred);
4286                         pnc   = get_Confirm_cmp(pred);
4287
4288                         NEW_ARR_A(ir_node *, in, n);
4289                         in[0] = get_Confirm_value(pred);
4290
4291                         for (i = 1; i < n; ++i) {
4292                                 pred = get_irn_n(phi, i);
4293
4294                                 if (! is_Confirm(pred) ||
4295                                         get_Confirm_bound(pred) != bound ||
4296                                         get_Confirm_cmp(pred) != pnc)
4297                                         return phi;
4298                                 in[i] = get_Confirm_value(pred);
4299                         }
4300                         /* move the Confirm nodes "behind" the Phi */
4301                         block = get_irn_n(phi, -1);
4302                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4303                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4304                 }
4305         }
4306         return phi;
4307 }  /* transform_node_Phi */
4308
4309 /**
4310  * Returns the operands of a commutative bin-op, if one operand is
4311  * a const, it is returned as the second one.
4312  */
4313 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4314         ir_node *op_a = get_binop_left(binop);
4315         ir_node *op_b = get_binop_right(binop);
4316
4317         assert(is_op_commutative(get_irn_op(binop)));
4318
4319         if (is_Const(op_a)) {
4320                 *a = op_b;
4321                 *c = op_a;
4322         } else {
4323                 *a = op_a;
4324                 *c = op_b;
4325         }
4326 }  /* get_comm_Binop_Ops */
4327
4328 /**
4329  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4330  * Such pattern may arise in bitfield stores.
4331  *
4332  * value  c4                  value      c4 & c2
4333  *    AND     c3                    AND           c1 | c3
4334  *        OR     c2      ===>               OR
4335  *           AND    c1
4336  *               OR
4337  *
4338  *
4339  * value  c2                 value  c1
4340  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4341  *        OR
4342  */
4343 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4344         ir_node *and, *c1;
4345         ir_node *or_l, *c2;
4346         ir_node *and_l, *c3;
4347         ir_node *value, *c4;
4348         ir_node *new_and, *new_const, *block;
4349         ir_mode *mode = get_irn_mode(or);
4350
4351         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4352
4353         while (1) {
4354                 get_comm_Binop_Ops(or, &and, &c1);
4355                 if (!is_Const(c1) || !is_And(and))
4356                         return or;
4357
4358                 get_comm_Binop_Ops(and, &or_l, &c2);
4359                 if (!is_Const(c2))
4360                         return or;
4361
4362                 tv1 = get_Const_tarval(c1);
4363                 tv2 = get_Const_tarval(c2);
4364
4365                 tv = tarval_or(tv1, tv2);
4366                 if (tarval_is_all_one(tv)) {
4367                         /* the AND does NOT clear a bit with isn't set by the OR */
4368                         set_Or_left(or, or_l);
4369                         set_Or_right(or, c1);
4370
4371                         /* check for more */
4372                         continue;
4373                 }
4374
4375                 if (!is_Or(or_l))
4376                         return or;
4377
4378                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4379                 if (!is_Const(c3) || !is_And(and_l))
4380                         return or;
4381
4382                 get_comm_Binop_Ops(and_l, &value, &c4);
4383                 if (!is_Const(c4))
4384                         return or;
4385
4386                 /* ok, found the pattern, check for conditions */
4387                 assert(mode == get_irn_mode(and));
4388                 assert(mode == get_irn_mode(or_l));
4389                 assert(mode == get_irn_mode(and_l));
4390
4391                 tv3 = get_Const_tarval(c3);
4392                 tv4 = get_Const_tarval(c4);
4393
4394                 tv = tarval_or(tv4, tv2);
4395                 if (!tarval_is_all_one(tv)) {
4396                         /* have at least one 0 at the same bit position */
4397                         return or;
4398                 }
4399
4400                 n_tv4 = tarval_not(tv4);
4401                 if (tv3 != tarval_and(tv3, n_tv4)) {
4402                         /* bit in the or_mask is outside the and_mask */
4403                         return or;
4404                 }
4405
4406                 n_tv2 = tarval_not(tv2);
4407                 if (tv1 != tarval_and(tv1, n_tv2)) {
4408                         /* bit in the or_mask is outside the and_mask */
4409                         return or;
4410                 }
4411
4412                 /* ok, all conditions met */
4413                 block = get_irn_n(or, -1);
4414
4415                 new_and = new_r_And(current_ir_graph, block,
4416                         value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4417
4418                 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4419
4420                 set_Or_left(or, new_and);
4421                 set_Or_right(or, new_const);
4422
4423                 /* check for more */
4424         }
4425 }  /* transform_node_Or_bf_store */
4426
4427 /**
4428  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4429  */
4430 static ir_node *transform_node_Or_Rot(ir_node *or) {
4431         ir_mode *mode = get_irn_mode(or);
4432         ir_node *shl, *shr, *block;
4433         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4434         tarval *tv1, *tv2;
4435
4436         if (! mode_is_int(mode))
4437                 return or;
4438
4439         shl = get_binop_left(or);
4440         shr = get_binop_right(or);
4441
4442         if (is_Shr(shl)) {
4443                 if (!is_Shl(shr))
4444                         return or;
4445
4446                 irn = shl;
4447                 shl = shr;
4448                 shr = irn;
4449         } else if (!is_Shl(shl)) {
4450                 return or;
4451         } else if (!is_Shr(shr)) {
4452                 return or;
4453         }
4454         x = get_Shl_left(shl);
4455         if (x != get_Shr_left(shr))
4456                 return or;
4457
4458         c1 = get_Shl_right(shl);
4459         c2 = get_Shr_right(shr);
4460         if (is_Const(c1) && is_Const(c2)) {
4461                 tv1 = get_Const_tarval(c1);
4462                 if (! tarval_is_long(tv1))
4463                         return or;
4464
4465                 tv2 = get_Const_tarval(c2);
4466                 if (! tarval_is_long(tv2))
4467                         return or;
4468
4469                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4470                                 != (int) get_mode_size_bits(mode))
4471                         return or;
4472
4473                 /* yet, condition met */
4474                 block = get_irn_n(or, -1);
4475
4476                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4477
4478                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4479                 return n;
4480         } else if (is_Sub(c1)) {
4481                 v   = c2;
4482                 sub = c1;
4483
4484                 if (get_Sub_right(sub) != v)
4485                         return or;
4486
4487                 c1 = get_Sub_left(sub);
4488                 if (!is_Const(c1))
4489                         return or;
4490
4491                 tv1 = get_Const_tarval(c1);
4492                 if (! tarval_is_long(tv1))
4493                         return or;
4494
4495                 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4496                         return or;
4497
4498                 /* yet, condition met */
4499                 block = get_nodes_block(or);
4500
4501                 /* a Rot right is not supported, so use a rot left */
4502                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
4503
4504                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4505                 return n;
4506         } else if (is_Sub(c2)) {
4507                 v   = c1;
4508                 sub = c2;
4509
4510                 c1 = get_Sub_left(sub);
4511                 if (!is_Const(c1))
4512                         return or;
4513
4514                 tv1 = get_Const_tarval(c1);
4515                 if (! tarval_is_long(tv1))
4516                         return or;
4517
4518                 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4519                         return or;
4520
4521                 /* yet, condition met */
4522                 block = get_irn_n(or, -1);
4523
4524                 /* a Rot Left */
4525                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4526
4527                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4528                 return n;
4529         }
4530
4531         return or;
4532 }  /* transform_node_Or_Rot */
4533
4534 /**
4535  * Transform an Or.
4536  */
4537 static ir_node *transform_node_Or(ir_node *n) {
4538         ir_node *c, *oldn = n;
4539         ir_node *a = get_Or_left(n);
4540         ir_node *b = get_Or_right(n);
4541         ir_mode *mode;
4542
4543         if (is_Not(a) && is_Not(b)) {
4544                 /* ~a | ~b = ~(a&b) */
4545                 ir_node *block = get_nodes_block(n);
4546
4547                 mode = get_irn_mode(n);
4548                 a = get_Not_op(a);
4549                 b = get_Not_op(b);
4550                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4551                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4552                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4553                 return n;
4554         }
4555
4556         /* we can evaluate 2 Projs of the same Cmp */
4557         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4558                 ir_node *pred_a = get_Proj_pred(a);
4559                 ir_node *pred_b = get_Proj_pred(b);
4560                 if (pred_a == pred_b) {
4561                         dbg_info *dbgi  = get_irn_dbg_info(n);
4562                         ir_node  *block = get_nodes_block(pred_a);
4563                         pn_Cmp pn_a     = get_Proj_proj(a);
4564                         pn_Cmp pn_b     = get_Proj_proj(b);
4565                         /* yes, we can simply calculate with pncs */
4566                         pn_Cmp new_pnc  = pn_a | pn_b;
4567
4568                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4569                                            new_pnc);
4570                 }
4571         }
4572
4573         mode = get_irn_mode(n);
4574         HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4575
4576         n = transform_node_Or_bf_store(n);
4577         n = transform_node_Or_Rot(n);
4578         if (n != oldn)
4579                 return n;
4580
4581         n = transform_bitwise_distributive(n, transform_node_Or);
4582
4583         return n;
4584 }  /* transform_node_Or */
4585
4586
4587 /* forward */
4588 static ir_node *transform_node(ir_node *n);
4589
4590 /**
4591  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4592  *
4593  * Should be moved to reassociation?
4594  */
4595 static ir_node *transform_node_shift(ir_node *n) {
4596         ir_node *left, *right;
4597         tarval *tv1, *tv2, *res;
4598         ir_mode *mode;
4599         int modulo_shf, flag;
4600
4601         left = get_binop_left(n);
4602
4603         /* different operations */
4604         if (get_irn_op(left) != get_irn_op(n))
4605                 return n;
4606
4607         right = get_binop_right(n);
4608         tv1 = value_of(right);
4609         if (tv1 == tarval_bad)
4610                 return n;
4611
4612         tv2 = value_of(get_binop_right(left));
4613         if (tv2 == tarval_bad)
4614                 return n;
4615
4616         res = tarval_add(tv1, tv2);
4617
4618         /* beware: a simple replacement works only, if res < modulo shift */
4619         mode = get_irn_mode(n);
4620
4621         flag = 0;
4622
4623         modulo_shf = get_mode_modulo_shift(mode);
4624         if (modulo_shf > 0) {
4625                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4626
4627                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4628                         flag = 1;
4629         } else
4630                 flag = 1;
4631
4632         if (flag) {
4633                 /* ok, we can replace it */
4634                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4635
4636                 in[0] = get_binop_left(left);
4637                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4638
4639                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4640
4641                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4642
4643                 return transform_node(irn);
4644         }
4645         return n;
4646 }  /* transform_node_shift */
4647
4648 /**
4649  * Transform a Shr.
4650  */
4651 static ir_node *transform_node_Shr(ir_node *n) {
4652         ir_node *c, *oldn = n;
4653         ir_node *a    = get_Shr_left(n);
4654         ir_node *b    = get_Shr_right(n);
4655         ir_mode *mode = get_irn_mode(n);
4656
4657         HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4658         return transform_node_shift(n);
4659 }  /* transform_node_Shr */
4660
4661 /**
4662  * Transform a Shrs.
4663  */
4664 static ir_node *transform_node_Shrs(ir_node *n) {
4665         ir_node *c, *oldn = n;
4666         ir_node *a    = get_Shrs_left(n);
4667         ir_node *b    = get_Shrs_right(n);
4668         ir_mode *mode = get_irn_mode(n);
4669
4670         HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4671         return transform_node_shift(n);
4672 }  /* transform_node_Shrs */
4673
4674 /**
4675  * Transform a Shl.
4676  */
4677 static ir_node *transform_node_Shl(ir_node *n) {
4678         ir_node *c, *oldn = n;
4679         ir_node *a    = get_Shl_left(n);
4680         ir_node *b    = get_Shl_right(n);
4681         ir_mode *mode = get_irn_mode(n);
4682
4683         HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4684         return transform_node_shift(n);
4685 }  /* transform_node_Shl */
4686
4687 /**
4688  * Transform a Rot.
4689  */
4690 static ir_node *transform_node_Rot(ir_node *n) {
4691         ir_node *c, *oldn = n;
4692         ir_node *a    = get_Rot_left(n);
4693         ir_node *b    = get_Rot_right(n);
4694         ir_mode *mode = get_irn_mode(n);
4695
4696         HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4697         return transform_node_shift(n);
4698 }  /* transform_node_Rot */
4699
4700 /**
4701  * Transform a Conv.
4702  */
4703 static ir_node *transform_node_Conv(ir_node *n) {
4704         ir_node *c, *oldn = n;
4705         ir_node *a = get_Conv_op(n);
4706
4707         if (is_const_Phi(a)) {
4708                 c = apply_conv_on_phi(a, get_irn_mode(n));
4709                 if (c) {
4710                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4711                         return c;
4712                 }
4713         }
4714
4715         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4716                 ir_mode *mode = get_irn_mode(n);
4717                 return new_r_Unknown(current_ir_graph, mode);
4718         }
4719
4720         return n;
4721 }  /* transform_node_Conv */
4722
4723 /**
4724  * Remove dead blocks and nodes in dead blocks
4725  * in keep alive list.  We do not generate a new End node.
4726  */
4727 static ir_node *transform_node_End(ir_node *n) {
4728         int i, j, n_keepalives = get_End_n_keepalives(n);
4729         ir_node **in;
4730
4731         NEW_ARR_A(ir_node *, in, n_keepalives);
4732
4733         for (i = j = 0; i < n_keepalives; ++i) {
4734                 ir_node *ka = get_End_keepalive(n, i);
4735                 if (is_Block(ka)) {
4736                         if (! is_Block_dead(ka)) {
4737                                 in[j++] = ka;
4738                         }
4739                         continue;
4740                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4741                         continue;
4742                 }
4743                 /* FIXME: beabi need to keep a Proj(M) */
4744                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4745                         in[j++] = ka;
4746         }
4747         if (j != n_keepalives)
4748                 set_End_keepalives(n, j, in);
4749         return n;
4750 }  /* transform_node_End */
4751
4752 /** returns 1 if a == -b */
4753 static int is_negated_value(ir_node *a, ir_node *b) {
4754         if (is_Minus(a) && get_Minus_op(a) == b)
4755                 return 1;
4756         if (is_Minus(b) && get_Minus_op(b) == a)
4757                 return 1;
4758         if (is_Sub(a) && is_Sub(b)) {
4759                 ir_node *a_left  = get_Sub_left(a);
4760                 ir_node *a_right = get_Sub_right(a);
4761                 ir_node *b_left  = get_Sub_left(b);
4762                 ir_node *b_right = get_Sub_right(b);
4763
4764                 if (a_left == b_right && a_right == b_left)
4765                         return 1;
4766         }
4767
4768         return 0;
4769 }
4770
4771 /**
4772  * Optimize a Mux into some simpler cases.
4773  */
4774 static ir_node *transform_node_Mux(ir_node *n) {
4775         ir_node *oldn = n, *sel = get_Mux_sel(n);
4776         ir_mode *mode = get_irn_mode(n);
4777
4778         if (mode == mode_b) {
4779                 ir_node  *t     = get_Mux_true(n);
4780                 ir_node  *f     = get_Mux_false(n);
4781                 dbg_info *dbg   = get_irn_dbg_info(n);
4782                 ir_node  *block = get_irn_n(n, -1);
4783                 ir_graph *irg   = current_ir_graph;
4784
4785                 if (is_Const(t)) {
4786                         tarval *tv_t = get_Const_tarval(t);
4787                         if (tv_t == tarval_b_true) {
4788                                 if (is_Const(f)) {
4789                                         /* Muxb(sel, true, false) = sel */
4790                                         assert(get_Const_tarval(f) == tarval_b_false);
4791                                         DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4792                                         return sel;
4793                                 } else {
4794                                         /* Muxb(sel, true, x) = Or(sel, x) */
4795                                         n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4796                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4797                                         return n;
4798                                 }
4799                         } else {
4800                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4801                                 assert(tv_t == tarval_b_false);
4802                                 if (is_Const(f)) {
4803                                         /* Muxb(sel, false, true) = Not(sel) */
4804                                         assert(get_Const_tarval(f) == tarval_b_true);
4805                                         DBG_OPT_ALGSIM0(oldn, not_sel, FS_OPT_MUX_NOT_BOOL);
4806                                         return not_sel;
4807                                 } else {
4808                                         /* Muxb(sel, false, x) = And(Not(sel), x) */
4809                                         n = new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4810                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ANDNOT_BOOL);
4811                                         return n;
4812                                 }
4813                         }
4814                 } else if (is_Const(f)) {
4815                         tarval *tv_f = get_Const_tarval(f);
4816                         if (tv_f == tarval_b_true) {
4817                                 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4818                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4819                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4820                                 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4821                                 return n;
4822                         } else {
4823                                 /* Muxb(sel, x, false) = And(sel, x) */
4824                                 assert(tv_f == tarval_b_false);
4825                                 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4826                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4827                                 return n;
4828                         }
4829                 }
4830         }
4831
4832         if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4833                 ir_node *cmp = get_Proj_pred(sel);
4834                 long     pn  = get_Proj_proj(sel);
4835                 ir_node *f   = get_Mux_false(n);
4836                 ir_node *t   = get_Mux_true(n);
4837
4838                 /*
4839                  * Note: normalization puts the constant on the right side,
4840                  * so we check only one case.
4841                  *
4842                  * Note further that these optimization work even for floating point
4843                  * with NaN's because -NaN == NaN.
4844                  * However, if +0 and -0 is handled differently, we cannot use the first
4845                  * one.
4846                  */
4847                 if (is_Cmp(cmp)) {
4848                         ir_node *cmp_r = get_Cmp_right(cmp);
4849                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4850                                 ir_node *block    = get_irn_n(n, -1);
4851
4852                                 if (is_negated_value(f, t)) {
4853                                         ir_node *cmp_left = get_Cmp_left(cmp);
4854
4855                                         /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4856                                         if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4857                                                 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4858                                         {
4859                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4860                                                                                  cmp_left, mode);
4861                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4862                                                 return n;
4863                                         /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4864                                         } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4865                                                 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4866                                         {
4867                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4868                                                                                  cmp_left, mode);
4869                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4870                                                                                                                  block, n, mode);
4871                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4872                                                 return n;
4873                                         }
4874                                 }
4875                         }
4876                 }
4877         }
4878         return arch_transform_node_Mux(n);
4879 }  /* transform_node_Mux */
4880
4881 /**
4882  * Optimize a Psi into some simpler cases.
4883  */
4884 static ir_node *transform_node_Psi(ir_node *n) {
4885         if (is_Mux(n))
4886                 return transform_node_Mux(n);
4887
4888         return n;
4889 }  /* transform_node_Psi */
4890
4891 /**
4892  * optimize sync nodes that have other syncs as input we simply add the inputs
4893  * of the other sync to our own inputs
4894  */
4895 static ir_node *transform_node_Sync(ir_node *n) {
4896         int arity = get_Sync_n_preds(n);
4897         int i;
4898
4899         for (i = 0; i < arity;) {
4900                 ir_node *pred = get_Sync_pred(n, i);
4901                 int      pred_arity;
4902                 int      j;
4903
4904                 if (!is_Sync(pred)) {
4905                         ++i;
4906                         continue;
4907                 }
4908
4909                 del_Sync_n(n, i);
4910                 --arity;
4911
4912                 pred_arity = get_Sync_n_preds(pred);
4913                 for (j = 0; j < pred_arity; ++j) {
4914                         ir_node *pred_pred = get_Sync_pred(pred, j);
4915                         int      k;
4916
4917                         for (k = 0;; ++k) {
4918                                 if (k >= arity) {
4919                                         add_irn_n(n, pred_pred);
4920                                         ++arity;
4921                                         break;
4922                                 }
4923                                 if (get_Sync_pred(n, k) == pred_pred) break;
4924                         }
4925                 }
4926         }
4927
4928         /* rehash the sync node */
4929         add_identities(current_ir_graph->value_table, n);
4930
4931         return n;
4932 }
4933
4934 /**
4935  * Tries several [inplace] [optimizing] transformations and returns an
4936  * equivalent node.  The difference to equivalent_node() is that these
4937  * transformations _do_ generate new nodes, and thus the old node must
4938  * not be freed even if the equivalent node isn't the old one.
4939  */
4940 static ir_node *transform_node(ir_node *n) {
4941         ir_node *oldn;
4942
4943         /*
4944          * Transform_node is the only "optimizing transformation" that might
4945          * return a node with a different opcode. We iterate HERE until fixpoint
4946          * to get the final result.
4947          */
4948         do {
4949                 oldn = n;
4950                 if (n->op->ops.transform_node)
4951                         n = n->op->ops.transform_node(n);
4952         } while (oldn != n);
4953
4954         return n;
4955 }  /* transform_node */
4956
4957 /**
4958  * Sets the default transform node operation for an ir_op_ops.
4959  *
4960  * @param code   the opcode for the default operation
4961  * @param ops    the operations initialized
4962  *
4963  * @return
4964  *    The operations.
4965  */
4966 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4967 {
4968 #define CASE(a)                                 \
4969         case iro_##a:                                 \
4970                 ops->transform_node  = transform_node_##a;  \
4971                 break
4972
4973         switch (code) {
4974         CASE(Add);
4975         CASE(Sub);
4976         CASE(Mul);
4977         CASE(Div);
4978         CASE(Mod);
4979         CASE(DivMod);
4980         CASE(Quot);
4981         CASE(Abs);
4982         CASE(Cond);
4983         CASE(And);
4984         CASE(Or);
4985         CASE(Eor);
4986         CASE(Minus);
4987         CASE(Not);
4988         CASE(Cast);
4989         CASE(Proj);
4990         CASE(Phi);
4991         CASE(Sel);
4992         CASE(Shr);
4993         CASE(Shrs);
4994         CASE(Shl);
4995         CASE(Rot);
4996         CASE(Conv);
4997         CASE(End);
4998         CASE(Mux);
4999         CASE(Psi);
5000         CASE(Sync);
5001         default:
5002           /* leave NULL */;
5003         }
5004
5005         return ops;
5006 #undef CASE
5007 }  /* firm_set_default_transform_node */
5008
5009
5010 /* **************** Common Subexpression Elimination **************** */
5011
5012 /** The size of the hash table used, should estimate the number of nodes
5013     in a graph. */
5014 #define N_IR_NODES 512
5015
5016 /** Compares the attributes of two Const nodes. */
5017 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5018         return (get_Const_tarval(a) != get_Const_tarval(b))
5019             || (get_Const_type(a) != get_Const_type(b));
5020 }  /* node_cmp_attr_Const */
5021
5022 /** Compares the attributes of two Proj nodes. */
5023 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5024         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5025 }  /* node_cmp_attr_Proj */
5026
5027 /** Compares the attributes of two Filter nodes. */
5028 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5029         return get_Filter_proj(a) != get_Filter_proj(b);
5030 }  /* node_cmp_attr_Filter */
5031
5032 /** Compares the attributes of two Alloc nodes. */
5033 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5034         const alloc_attr *pa = get_irn_alloc_attr(a);
5035         const alloc_attr *pb = get_irn_alloc_attr(b);
5036         return (pa->where != pb->where) || (pa->type != pb->type);
5037 }  /* node_cmp_attr_Alloc */
5038
5039 /** Compares the attributes of two Free nodes. */
5040 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5041         const free_attr *pa = get_irn_free_attr(a);
5042         const free_attr *pb = get_irn_free_attr(b);
5043         return (pa->where != pb->where) || (pa->type != pb->type);
5044 }  /* node_cmp_attr_Free */
5045
5046 /** Compares the attributes of two SymConst nodes. */
5047 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5048         const symconst_attr *pa = get_irn_symconst_attr(a);
5049         const symconst_attr *pb = get_irn_symconst_attr(b);
5050         return (pa->kind       != pb->kind)
5051             || (pa->sym.type_p != pb->sym.type_p)
5052             || (pa->tp         != pb->tp);
5053 }  /* node_cmp_attr_SymConst */
5054
5055 /** Compares the attributes of two Call nodes. */
5056 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5057         return get_irn_call_attr(a) != get_irn_call_attr(b);
5058 }  /* node_cmp_attr_Call */
5059
5060 /** Compares the attributes of two Sel nodes. */
5061 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5062         const ir_entity *a_ent = get_Sel_entity(a);
5063         const ir_entity *b_ent = get_Sel_entity(b);
5064         return
5065                 (a_ent->kind    != b_ent->kind)    ||
5066                 (a_ent->name    != b_ent->name)    ||
5067                 (a_ent->owner   != b_ent->owner)   ||
5068                 (a_ent->ld_name != b_ent->ld_name) ||
5069                 (a_ent->type    != b_ent->type);
5070 }  /* node_cmp_attr_Sel */
5071
5072 /** Compares the attributes of two Phi nodes. */
5073 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5074         /* we can only enter this function if both nodes have the same number of inputs,
5075            hence it is enough to check if one of them is a Phi0 */
5076         if (is_Phi0(a)) {
5077                 /* check the Phi0 pos attribute */
5078                 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5079         }
5080         return 0;
5081 }  /* node_cmp_attr_Phi */
5082
5083 /** Compares the attributes of two Conv nodes. */
5084 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5085         return get_Conv_strict(a) != get_Conv_strict(b);
5086 }  /* node_cmp_attr_Conv */
5087
5088 /** Compares the attributes of two Cast nodes. */
5089 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5090         return get_Cast_type(a) != get_Cast_type(b);
5091 }  /* node_cmp_attr_Cast */
5092
5093 /** Compares the attributes of two Load nodes. */
5094 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5095         if (get_Load_volatility(a) == volatility_is_volatile ||
5096             get_Load_volatility(b) == volatility_is_volatile)
5097                 /* NEVER do CSE on volatile Loads */
5098                 return 1;
5099         /* do not CSE Loads with different alignment. Be conservative. */
5100         if (get_Load_align(a) != get_Load_align(b))
5101                 return 1;
5102
5103         return get_Load_mode(a) != get_Load_mode(b);
5104 }  /* node_cmp_attr_Load */
5105
5106 /** Compares the attributes of two Store nodes. */
5107 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5108         /* do not CSE Stores with different alignment. Be conservative. */
5109         if (get_Store_align(a) != get_Store_align(b))
5110                 return 1;
5111
5112         /* NEVER do CSE on volatile Stores */
5113         return (get_Store_volatility(a) == volatility_is_volatile ||
5114                 get_Store_volatility(b) == volatility_is_volatile);
5115 }  /* node_cmp_attr_Store */
5116
5117 /** Compares two exception attributes */
5118 static int node_cmp_exception(ir_node *a, ir_node *b) {
5119         const except_attr *ea = get_irn_except_attr(a);
5120         const except_attr *eb = get_irn_except_attr(b);
5121
5122         return ea->pin_state != eb->pin_state;
5123 }
5124
5125 #define node_cmp_attr_Bound  node_cmp_exception
5126
5127 /** Compares the attributes of two Div nodes. */
5128 static int node_cmp_attr_Div(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                    ma->no_remainder  != mb->no_remainder;
5134 }  /* node_cmp_attr_Div */
5135
5136 /** Compares the attributes of two DivMod nodes. */
5137 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5138         const divmod_attr *ma = get_irn_divmod_attr(a);
5139         const divmod_attr *mb = get_irn_divmod_attr(b);
5140         return ma->exc.pin_state != mb->exc.pin_state ||
5141                    ma->res_mode      != mb->res_mode;
5142 }  /* node_cmp_attr_DivMod */
5143
5144 /** Compares the attributes of two Mod nodes. */
5145 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5146         const divmod_attr *ma = get_irn_divmod_attr(a);
5147         const divmod_attr *mb = get_irn_divmod_attr(b);
5148         return ma->exc.pin_state != mb->exc.pin_state ||
5149                    ma->res_mode      != mb->res_mode;
5150 }  /* node_cmp_attr_Mod */
5151
5152 /** Compares the attributes of two Quot nodes. */
5153 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5154         const divmod_attr *ma = get_irn_divmod_attr(a);
5155         const divmod_attr *mb = get_irn_divmod_attr(b);
5156         return ma->exc.pin_state != mb->exc.pin_state ||
5157                    ma->res_mode      != mb->res_mode;
5158 }  /* node_cmp_attr_Quot */
5159
5160 /** Compares the attributes of two Confirm nodes. */
5161 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5162         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5163 }  /* node_cmp_attr_Confirm */
5164
5165 /** Compares the attributes of two ASM nodes. */
5166 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5167         int i, n;
5168         const ir_asm_constraint *ca;
5169         const ir_asm_constraint *cb;
5170         ident **cla, **clb;
5171
5172         if (get_ASM_text(a) != get_ASM_text(b))
5173                 return 1;
5174
5175         /* Should we really check the constraints here? Should be better, but is strange. */
5176         n = get_ASM_n_input_constraints(a);
5177         if (n != get_ASM_n_input_constraints(b))
5178                 return 0;
5179
5180         ca = get_ASM_input_constraints(a);
5181         cb = get_ASM_input_constraints(b);
5182         for (i = 0; i < n; ++i) {
5183                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5184                         return 1;
5185         }
5186
5187         n = get_ASM_n_output_constraints(a);
5188         if (n != get_ASM_n_output_constraints(b))
5189                 return 0;
5190
5191         ca = get_ASM_output_constraints(a);
5192         cb = get_ASM_output_constraints(b);
5193         for (i = 0; i < n; ++i) {
5194                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5195                         return 1;
5196         }
5197
5198         n = get_ASM_n_clobbers(a);
5199         if (n != get_ASM_n_clobbers(b))
5200                 return 0;
5201
5202         cla = get_ASM_clobbers(a);
5203         clb = get_ASM_clobbers(b);
5204         for (i = 0; i < n; ++i) {
5205                 if (cla[i] != clb[i])
5206                         return 1;
5207         }
5208         return 0;
5209 }  /* node_cmp_attr_ASM */
5210
5211 /**
5212  * Set the default node attribute compare operation for an ir_op_ops.
5213  *
5214  * @param code   the opcode for the default operation
5215  * @param ops    the operations initialized
5216  *
5217  * @return
5218  *    The operations.
5219  */
5220 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5221 {
5222 #define CASE(a)                              \
5223         case iro_##a:                              \
5224                 ops->node_cmp_attr  = node_cmp_attr_##a; \
5225                 break
5226
5227         switch (code) {
5228         CASE(Const);
5229         CASE(Proj);
5230         CASE(Filter);
5231         CASE(Alloc);
5232         CASE(Free);
5233         CASE(SymConst);
5234         CASE(Call);
5235         CASE(Sel);
5236         CASE(Phi);
5237         CASE(Conv);
5238         CASE(Cast);
5239         CASE(Load);
5240         CASE(Store);
5241         CASE(Confirm);
5242         CASE(ASM);
5243         CASE(Div);
5244         CASE(DivMod);
5245         CASE(Mod);
5246         CASE(Quot);
5247         CASE(Bound);
5248         /* FIXME CopyB */
5249         default:
5250           /* leave NULL */;
5251         }
5252
5253         return ops;
5254 #undef CASE
5255 }  /* firm_set_default_node_cmp_attr */
5256
5257 /*
5258  * Compare function for two nodes in the value table. Gets two
5259  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
5260  */
5261 int identities_cmp(const void *elt, const void *key) {
5262         ir_node *a = (ir_node *)elt;
5263         ir_node *b = (ir_node *)key;
5264         int i, irn_arity_a;
5265
5266         if (a == b) return 0;
5267
5268         if ((get_irn_op(a) != get_irn_op(b)) ||
5269             (get_irn_mode(a) != get_irn_mode(b))) return 1;
5270
5271         /* compare if a's in and b's in are of equal length */
5272         irn_arity_a = get_irn_intra_arity(a);
5273         if (irn_arity_a != get_irn_intra_arity(b))
5274                 return 1;
5275
5276         if (get_irn_pinned(a) == op_pin_state_pinned) {
5277                 /* for pinned nodes, the block inputs must be equal */
5278                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5279                         return 1;
5280         } else if (! get_opt_global_cse()) {
5281                 /* for block-local CSE both nodes must be in the same MacroBlock */
5282                 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5283                         return 1;
5284         }
5285
5286         /* compare a->in[0..ins] with b->in[0..ins] */
5287         for (i = 0; i < irn_arity_a; i++)
5288                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5289                         return 1;
5290
5291         /*
5292          * here, we already now that the nodes are identical except their
5293          * attributes
5294          */
5295         if (a->op->ops.node_cmp_attr)
5296                 return a->op->ops.node_cmp_attr(a, b);
5297
5298         return 0;
5299 }  /* identities_cmp */
5300
5301 /*
5302  * Calculate a hash value of a node.
5303  */
5304 unsigned ir_node_hash(ir_node *node) {
5305         unsigned h;
5306         int i, irn_arity;
5307
5308         if (node->op == op_Const) {
5309                 /* special value for const, as they only differ in their tarval. */
5310                 h = HASH_PTR(node->attr.con.tv);
5311                 h = 9*h + HASH_PTR(get_irn_mode(node));
5312         } else if (node->op == op_SymConst) {
5313                 /* special value for const, as they only differ in their symbol. */
5314                 h = HASH_PTR(node->attr.symc.sym.type_p);
5315                 h = 9*h + HASH_PTR(get_irn_mode(node));
5316         } else {
5317
5318                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5319                 h = irn_arity = get_irn_intra_arity(node);
5320
5321                 /* consider all in nodes... except the block if not a control flow. */
5322                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
5323                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5324                 }
5325
5326                 /* ...mode,... */
5327                 h = 9*h + HASH_PTR(get_irn_mode(node));
5328                 /* ...and code */
5329                 h = 9*h + HASH_PTR(get_irn_op(node));
5330         }
5331
5332         return h;
5333 }  /* ir_node_hash */
5334
5335 pset *new_identities(void) {
5336         return new_pset(identities_cmp, N_IR_NODES);
5337 }  /* new_identities */
5338
5339 void del_identities(pset *value_table) {
5340         del_pset(value_table);
5341 }  /* del_identities */
5342
5343 /**
5344  * Normalize a node by putting constants (and operands with larger
5345  * node index) on the right (operator side).
5346  *
5347  * @param n   The node to normalize
5348  */
5349 static void normalize_node(ir_node *n) {
5350         if (is_op_commutative(get_irn_op(n))) {
5351                 ir_node *l = get_binop_left(n);
5352                 ir_node *r = get_binop_right(n);
5353
5354                 /* For commutative operators perform  a OP b == b OP a but keep
5355                  * constants on the RIGHT side. This helps greatly in some
5356                  * optimizations.  Moreover we use the idx number to make the form
5357                  * deterministic. */
5358                 if (!operands_are_normalized(l, r)) {
5359                         set_binop_left(n, r);
5360                         set_binop_right(n, l);
5361                 }
5362         }
5363 }  /* normalize_node */
5364
5365 /**
5366  * Update the nodes after a match in the value table. If both nodes have
5367  * the same MacroBlock but different Blocks, we must ensure that the node
5368  * with the dominating Block (the node that is near to the MacroBlock header
5369  * is stored in the table.
5370  * Because a MacroBlock has only one "non-exception" flow, we don't need
5371  * dominance info here: We known, that one block must dominate the other and
5372  * following the only block input will allow to find it.
5373  */
5374 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5375         ir_node *known_blk, *new_block, *block, *mbh;
5376
5377         if (get_opt_global_cse()) {
5378                 /* Block inputs are meaning less */
5379                 return;
5380         }
5381         known_blk = get_irn_n(known_irn, -1);
5382         new_block = get_irn_n(new_ir_node, -1);
5383         if (known_blk == new_block) {
5384                 /* already in the same block */
5385                 return;
5386         }
5387         /*
5388          * We expect the typical case when we built the graph. In that case, the
5389          * known_irn is already the upper one, so checking this should be faster.
5390          */
5391         block = new_block;
5392         mbh   = get_Block_MacroBlock(new_block);
5393         for (;;) {
5394                 if (block == known_blk) {
5395                         /* ok, we have found it: known_block dominates new_block as expected */
5396                         return;
5397                 }
5398                 if (block == mbh) {
5399                         /*
5400                          * We have reached the MacroBlock header NOT founding
5401                          * the known_block. new_block must dominate known_block.
5402                          * Update known_irn.
5403                          */
5404                         set_irn_n(known_irn, -1, new_block);
5405                         return;
5406                 }
5407                 assert(get_Block_n_cfgpreds(block) == 1);
5408                 block = get_Block_cfgpred_block(block, 0);
5409         }
5410 }  /* update_value_table */
5411
5412 /**
5413  * Return the canonical node computing the same value as n.
5414  *
5415  * @param value_table  The value table
5416  * @param n            The node to lookup
5417  *
5418  * Looks up the node in a hash table.
5419  *
5420  * For Const nodes this is performed in the constructor, too.  Const
5421  * nodes are extremely time critical because of their frequent use in
5422  * constant string arrays.
5423  */
5424 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5425         ir_node *o = NULL;
5426
5427         if (!value_table) return n;
5428
5429         normalize_node(n);
5430
5431         o = pset_find(value_table, n, ir_node_hash(n));
5432         if (o == NULL)
5433                 return n;
5434
5435         update_known_irn(o, n);
5436         DBG_OPT_CSE(n, o);
5437
5438         return o;
5439 }  /* identify */
5440
5441 /**
5442  * During construction we set the op_pin_state_pinned flag in the graph right when the
5443  * optimization is performed.  The flag turning on procedure global cse could
5444  * be changed between two allocations.  This way we are safe.
5445  *
5446  * @param value_table  The value table
5447  * @param n            The node to lookup
5448  */
5449 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5450         ir_node *old = n;
5451
5452         n = identify(value_table, n);
5453         if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5454                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5455         return n;
5456 }  /* identify_cons */
5457
5458 /*
5459  * Return the canonical node computing the same value as n.
5460  * Looks up the node in a hash table, enters it in the table
5461  * if it isn't there yet.
5462  *
5463  * @param value_table  the HashSet containing all nodes in the
5464  *                     current IR graph
5465  * @param n            the node to look up
5466  *
5467  * @return a node that computes the same value as n or n if no such
5468  *         node could be found
5469  */
5470 ir_node *identify_remember(pset *value_table, ir_node *n) {
5471         ir_node *o = NULL;
5472
5473         if (!value_table) return n;
5474
5475         normalize_node(n);
5476         /* lookup or insert in hash table with given hash key. */
5477         o = pset_insert(value_table, n, ir_node_hash(n));
5478
5479         if (o != n) {
5480                 update_known_irn(o, n);
5481                 DBG_OPT_CSE(n, o);
5482         }
5483
5484         return o;
5485 }  /* identify_remember */
5486
5487 /* Add a node to the identities value table. */
5488 void add_identities(pset *value_table, ir_node *node) {
5489         if (get_opt_cse() && is_no_Block(node))
5490                 identify_remember(value_table, node);
5491 }  /* add_identities */
5492
5493 /* Visit each node in the value table of a graph. */
5494 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5495         ir_node *node;
5496         ir_graph *rem = current_ir_graph;
5497
5498         current_ir_graph = irg;
5499         foreach_pset(irg->value_table, node)
5500                 visit(node, env);
5501         current_ir_graph = rem;
5502 }  /* visit_all_identities */
5503
5504 /**
5505  * Garbage in, garbage out. If a node has a dead input, i.e., the
5506  * Bad node is input to the node, return the Bad node.
5507  */
5508 static ir_node *gigo(ir_node *node) {
5509         int i, irn_arity;
5510         ir_op *op = get_irn_op(node);
5511
5512         /* remove garbage blocks by looking at control flow that leaves the block
5513            and replacing the control flow by Bad. */
5514         if (get_irn_mode(node) == mode_X) {
5515                 ir_node *block = get_nodes_block(skip_Proj(node));
5516
5517                 /* Don't optimize nodes in immature blocks. */
5518                 if (!get_Block_matured(block))
5519                         return node;
5520                 /* Don't optimize End, may have Bads. */
5521                 if (op == op_End) return node;
5522
5523                 if (is_Block(block)) {
5524                         if (is_Block_dead(block)) {
5525                                 /* control flow from dead block is dead */
5526                                 return new_Bad();
5527                         }
5528
5529                         for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5530                                 if (!is_Bad(get_irn_n(block, i)))
5531                                         break;
5532                         }
5533                         if (i < 0) {
5534                                 ir_graph *irg = get_irn_irg(block);
5535                                 /* the start block is never dead */
5536                                 if (block != get_irg_start_block(irg)
5537                                         && block != get_irg_end_block(irg)) {
5538                                         /*
5539                                          * Do NOT kill control flow without setting
5540                                          * the block to dead of bad things can happen:
5541                                          * We get a Block that is not reachable be irg_block_walk()
5542                                          * but can be found by irg_walk()!
5543                                          */
5544                                         set_Block_dead(block);
5545                                         return new_Bad();
5546                                 }
5547                         }
5548                 }
5549         }
5550
5551         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5552            blocks predecessors is dead. */
5553         if (op != op_Block && op != op_Phi && op != op_Tuple) {
5554                 irn_arity = get_irn_arity(node);
5555
5556                 /*
5557                  * Beware: we can only read the block of a non-floating node.
5558                  */
5559                 if (is_irn_pinned_in_irg(node) &&
5560                         is_Block_dead(get_nodes_block(skip_Proj(node))))
5561                         return new_Bad();
5562
5563                 for (i = 0; i < irn_arity; i++) {
5564                         ir_node *pred = get_irn_n(node, i);
5565
5566                         if (is_Bad(pred))
5567                                 return new_Bad();
5568 #if 0
5569                         /* Propagating Unknowns here seems to be a bad idea, because
5570                            sometimes we need a node as a input and did not want that
5571                            it kills it's user.
5572                            However, it might be useful to move this into a later phase
5573                            (if you think that optimizing such code is useful). */
5574                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5575                                 return new_Unknown(get_irn_mode(node));
5576 #endif
5577                 }
5578         }
5579 #if 0
5580         /* With this code we violate the agreement that local_optimize
5581            only leaves Bads in Block, Phi and Tuple nodes. */
5582         /* If Block has only Bads as predecessors it's garbage. */
5583         /* If Phi has only Bads as predecessors it's garbage. */
5584         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
5585                 irn_arity = get_irn_arity(node);
5586                 for (i = 0; i < irn_arity; i++) {
5587                         if (!is_Bad(get_irn_n(node, i))) break;
5588                 }
5589                 if (i == irn_arity) node = new_Bad();
5590         }
5591 #endif
5592         return node;
5593 }  /* gigo */
5594
5595 /**
5596  * These optimizations deallocate nodes from the obstack.
5597  * It can only be called if it is guaranteed that no other nodes
5598  * reference this one, i.e., right after construction of a node.
5599  *
5600  * @param n   The node to optimize
5601  *
5602  * current_ir_graph must be set to the graph of the node!
5603  */
5604 ir_node *optimize_node(ir_node *n) {
5605         tarval *tv;
5606         ir_node *oldn = n;
5607         ir_opcode iro = get_irn_opcode(n);
5608
5609         /* Always optimize Phi nodes: part of the construction. */
5610         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5611
5612         /* constant expression evaluation / constant folding */
5613         if (get_opt_constant_folding()) {
5614                 /* neither constants nor Tuple values can be evaluated */
5615                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5616                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5617                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5618                         /* try to evaluate */
5619                         tv = computed_value(n);
5620                         if (tv != tarval_bad) {
5621                                 ir_node *nw;
5622                                 ir_type *old_tp = get_irn_type(n);
5623                                 int i, arity = get_irn_arity(n);
5624                                 int node_size;
5625
5626                                 /*
5627                                  * Try to recover the type of the new expression.
5628                                  */
5629                                 for (i = 0; i < arity && !old_tp; ++i)
5630                                         old_tp = get_irn_type(get_irn_n(n, i));
5631
5632                                 /*
5633                                  * we MUST copy the node here temporary, because it's still needed
5634                                  * for DBG_OPT_CSTEVAL
5635                                  */
5636                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
5637                                 oldn = alloca(node_size);
5638
5639                                 memcpy(oldn, n, node_size);
5640                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5641
5642                                 /* ARG, copy the in array, we need it for statistics */
5643                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5644
5645                                 /* note the inplace edges module */
5646                                 edges_node_deleted(n, current_ir_graph);
5647
5648                                 /* evaluation was successful -- replace the node. */
5649                                 irg_kill_node(current_ir_graph, n);
5650                                 nw = new_Const(get_tarval_mode(tv), tv);
5651
5652                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5653                                         set_Const_type(nw, old_tp);
5654                                 DBG_OPT_CSTEVAL(oldn, nw);
5655                                 tarval_enable_fp_ops(old_fp_mode);
5656                                 return nw;
5657                         }
5658                         tarval_enable_fp_ops(old_fp_mode);
5659                 }
5660         }
5661
5662         /* remove unnecessary nodes */
5663         if (get_opt_constant_folding() ||
5664             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5665             (iro == iro_Id)   ||
5666             (iro == iro_Proj) ||
5667             (iro == iro_Block)  )  /* Flags tested local. */
5668                 n = equivalent_node(n);
5669
5670         /* Common Subexpression Elimination.
5671          *
5672          * Checks whether n is already available.
5673          * The block input is used to distinguish different subexpressions. Right
5674          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5675          * subexpressions within a block.
5676          */
5677         if (get_opt_cse())
5678                 n = identify_cons(current_ir_graph->value_table, n);
5679
5680         if (n != oldn) {
5681                 edges_node_deleted(oldn, current_ir_graph);
5682
5683                 /* We found an existing, better node, so we can deallocate the old node. */
5684                 irg_kill_node(current_ir_graph, oldn);
5685                 return n;
5686         }
5687
5688         /* Some more constant expression evaluation that does not allow to
5689            free the node. */
5690         iro = get_irn_opcode(n);
5691         if (get_opt_constant_folding() ||
5692             (iro == iro_Cond) ||
5693             (iro == iro_Proj))     /* Flags tested local. */
5694                 n = transform_node(n);
5695
5696         /* Remove nodes with dead (Bad) input.
5697            Run always for transformation induced Bads. */
5698         n = gigo (n);
5699
5700         /* Now we have a legal, useful node. Enter it in hash table for CSE */
5701         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5702                 n = identify_remember(current_ir_graph->value_table, n);
5703         }
5704
5705         return n;
5706 }  /* optimize_node */
5707
5708
5709 /**
5710  * These optimizations never deallocate nodes (in place).  This can cause dead
5711  * nodes lying on the obstack.  Remove these by a dead node elimination,
5712  * i.e., a copying garbage collection.
5713  */
5714 ir_node *optimize_in_place_2(ir_node *n) {
5715         tarval *tv;
5716         ir_node *oldn = n;
5717         ir_opcode iro = get_irn_opcode(n);
5718
5719         if (!get_opt_optimize() && !is_Phi(n)) return n;
5720
5721         /* constant expression evaluation / constant folding */
5722         if (get_opt_constant_folding()) {
5723                 /* neither constants nor Tuple values can be evaluated */
5724                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5725                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5726                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5727                         /* try to evaluate */
5728                         tv = computed_value(n);
5729                         if (tv != tarval_bad) {
5730                                 /* evaluation was successful -- replace the node. */
5731                                 ir_type *old_tp = get_irn_type(n);
5732                                 int i, arity = get_irn_arity(n);
5733
5734                                 /*
5735                                  * Try to recover the type of the new expression.
5736                                  */
5737                                 for (i = 0; i < arity && !old_tp; ++i)
5738                                         old_tp = get_irn_type(get_irn_n(n, i));
5739
5740                                 n = new_Const(get_tarval_mode(tv), tv);
5741
5742                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5743                                         set_Const_type(n, old_tp);
5744
5745                                 DBG_OPT_CSTEVAL(oldn, n);
5746                                 tarval_enable_fp_ops(old_fp_mode);
5747                                 return n;
5748                         }
5749                         tarval_enable_fp_ops(old_fp_mode);
5750                 }
5751         }
5752
5753         /* remove unnecessary nodes */
5754         if (get_opt_constant_folding() ||
5755             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5756             (iro == iro_Id)   ||   /* ... */
5757             (iro == iro_Proj) ||   /* ... */
5758             (iro == iro_Block)  )  /* Flags tested local. */
5759                 n = equivalent_node(n);
5760
5761         /** common subexpression elimination **/
5762         /* Checks whether n is already available. */
5763         /* The block input is used to distinguish different subexpressions.  Right
5764            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5765            subexpressions within a block. */
5766         if (get_opt_cse()) {
5767                 n = identify(current_ir_graph->value_table, n);
5768         }
5769
5770         /* Some more constant expression evaluation. */
5771         iro = get_irn_opcode(n);
5772         if (get_opt_constant_folding() ||
5773                 (iro == iro_Cond) ||
5774                 (iro == iro_Proj))     /* Flags tested local. */
5775                 n = transform_node(n);
5776
5777         /* Remove nodes with dead (Bad) input.
5778            Run always for transformation induced Bads.  */
5779         n = gigo(n);
5780
5781         /* Now we can verify the node, as it has no dead inputs any more. */
5782         irn_vrfy(n);
5783
5784         /* Now we have a legal, useful node. Enter it in hash table for cse.
5785            Blocks should be unique anyways.  (Except the successor of start:
5786            is cse with the start block!) */
5787         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5788                 n = identify_remember(current_ir_graph->value_table, n);
5789
5790         return n;
5791 }  /* optimize_in_place_2 */
5792
5793 /**
5794  * Wrapper for external use, set proper status bits after optimization.
5795  */
5796 ir_node *optimize_in_place(ir_node *n) {
5797         /* Handle graph state */
5798         assert(get_irg_phase_state(current_ir_graph) != phase_building);
5799
5800         if (get_opt_global_cse())
5801                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5802         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5803                 set_irg_outs_inconsistent(current_ir_graph);
5804
5805         /* FIXME: Maybe we could also test whether optimizing the node can
5806            change the control graph. */
5807         set_irg_doms_inconsistent(current_ir_graph);
5808         return optimize_in_place_2(n);
5809 }  /* optimize_in_place */
5810
5811 /*
5812  * Sets the default operation for an ir_ops.
5813  */
5814 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5815         ops = firm_set_default_computed_value(code, ops);
5816         ops = firm_set_default_equivalent_node(code, ops);
5817         ops = firm_set_default_transform_node(code, ops);
5818         ops = firm_set_default_node_cmp_attr(code, ops);
5819         ops = firm_set_default_get_type(code, ops);
5820         ops = firm_set_default_get_type_attr(code, ops);
5821         ops = firm_set_default_get_entity_attr(code, ops);
5822
5823         return ops;
5824 }  /* firm_set_default_operations */