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