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