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