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