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