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