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