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