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