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