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