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