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