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