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