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