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