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