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