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