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