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