Fix setoutfile/irgname debugger commands.
[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 = get_mode_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 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2465
2466 /**
2467  * for associative operations fold:
2468  *   op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2469  * This is a "light" version of the reassociation phase
2470  */
2471 static ir_node *fold_constant_associativity(ir_node *node,
2472                                             tv_fold_binop_func fold)
2473 {
2474         ir_graph  *irg;
2475         ir_op     *op;
2476         ir_node   *left;
2477         ir_node   *right = get_binop_right(node);
2478         ir_node   *left_right;
2479         ir_node   *left_left;
2480         ir_tarval *c0;
2481         ir_tarval *c1;
2482         ir_tarval *new_c;
2483         ir_node   *new_const;
2484         ir_node   *new_node;
2485         if (!is_Const(right))
2486                 return node;
2487
2488         op   = get_irn_op(node);
2489         left = get_binop_left(node);
2490         if (get_irn_op(left) != op)
2491                 return node;
2492
2493         left_right = get_binop_right(left);
2494         if (!is_Const(left_right))
2495                 return node;
2496
2497         left_left = get_binop_left(left);
2498         c0        = get_Const_tarval(left_right);
2499         c1        = get_Const_tarval(right);
2500         irg       = get_irn_irg(node);
2501         if (get_tarval_mode(c0) != get_tarval_mode(c1))
2502                 return node;
2503         new_c     = fold(c0, c1);
2504         if (new_c == tarval_bad)
2505                 return node;
2506         new_const = new_r_Const(irg, new_c);
2507         new_node  = exact_copy(node);
2508         set_binop_left(new_node, left_left);
2509         set_binop_right(new_node, new_const);
2510         return new_node;
2511 }
2512
2513 /**
2514  * Transform an Or.
2515  */
2516 static ir_node *transform_node_Or_(ir_node *n)
2517 {
2518         ir_node *oldn = n;
2519         ir_node *a    = get_binop_left(n);
2520         ir_node *b    = get_binop_right(n);
2521         ir_node *c;
2522         ir_mode *mode;
2523
2524         n = fold_constant_associativity(n, tarval_or);
2525         if (n != oldn)
2526                 return n;
2527
2528         if (is_Not(a) && is_Not(b)) {
2529                 /* ~a | ~b = ~(a&b) */
2530                 ir_node *block = get_nodes_block(n);
2531
2532                 mode = get_irn_mode(n);
2533                 a = get_Not_op(a);
2534                 b = get_Not_op(b);
2535                 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2536                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2537                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2538                 return n;
2539         }
2540
2541         /* we can combine the relations of two compares with the same operands */
2542         if (is_Cmp(a) && is_Cmp(b)) {
2543                 ir_node *a_left  = get_Cmp_left(a);
2544                 ir_node *a_right = get_Cmp_right(a);
2545                 ir_node *b_left  = get_Cmp_left(b);
2546                 ir_node *b_right = get_Cmp_right(b);
2547                 if (a_left == b_left && b_left == b_right) {
2548                         dbg_info   *dbgi         = get_irn_dbg_info(n);
2549                         ir_node    *block        = get_nodes_block(n);
2550                         ir_relation a_relation   = get_Cmp_relation(a);
2551                         ir_relation b_relation   = get_Cmp_relation(b);
2552                         ir_relation new_relation = a_relation | b_relation;
2553                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2554                 }
2555                 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2556                 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2557                     && !mode_is_float(get_irn_mode(a_left))
2558                     && !mode_is_float(get_irn_mode(b_left))) {
2559                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2560                                 ir_graph *irg    = get_irn_irg(n);
2561                                 dbg_info *dbgi   = get_irn_dbg_info(n);
2562                                 ir_node  *block  = get_nodes_block(n);
2563                                 ir_mode  *a_mode = get_irn_mode(a_left);
2564                                 ir_mode  *b_mode = get_irn_mode(b_left);
2565                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2566                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2567                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
2568                                 ir_node  *or     = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2569                                 ir_node  *zero   = create_zero_const(irg, b_mode);
2570                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2571                         }
2572                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2573                                 ir_graph *irg    = get_irn_irg(n);
2574                                 dbg_info *dbgi   = get_irn_dbg_info(n);
2575                                 ir_node  *block  = get_nodes_block(n);
2576                                 ir_mode  *a_mode = get_irn_mode(a_left);
2577                                 ir_mode  *b_mode = get_irn_mode(b_left);
2578                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2579                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2580                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
2581                                 ir_node  *or     = new_rd_Or(dbgi, block, xora, conv, a_mode);
2582                                 ir_node  *zero   = create_zero_const(irg, a_mode);
2583                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2584                         }
2585                 }
2586         }
2587
2588         mode = get_irn_mode(n);
2589         HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2590
2591         n = transform_node_Or_bf_store(n);
2592         if (n != oldn)
2593                 return n;
2594         n = transform_node_Or_Rotl(n);
2595         if (n != oldn)
2596                 return n;
2597
2598         n = transform_bitwise_distributive(n, transform_node_Or_);
2599         if (n != oldn)
2600                 return n;
2601         n = transform_node_bitop_shift(n);
2602         if (n != oldn)
2603                 return n;
2604
2605         return n;
2606 }
2607
2608 static ir_node *transform_node_Or(ir_node *n)
2609 {
2610         if (is_Or_Eor_Add(n)) {
2611                 dbg_info *dbgi  = get_irn_dbg_info(n);
2612                 ir_node  *block = get_nodes_block(n);
2613                 ir_node  *left  = get_Or_left(n);
2614                 ir_node  *right = get_Or_right(n);
2615                 ir_mode  *mode  = get_irn_mode(n);
2616                 return new_rd_Add(dbgi, block, left, right, mode);
2617         }
2618         return transform_node_Or_(n);
2619 }
2620
2621 /**
2622  * Transform an Eor.
2623  */
2624 static ir_node *transform_node_Eor_(ir_node *n)
2625 {
2626         ir_node *oldn = n;
2627         ir_node *a    = get_binop_left(n);
2628         ir_node *b    = get_binop_right(n);
2629         ir_mode *mode = get_irn_mode(n);
2630         ir_node *c;
2631
2632         n = fold_constant_associativity(n, tarval_eor);
2633         if (n != oldn)
2634                 return n;
2635
2636         /* we can combine the relations of two compares with the same operands */
2637         if (is_Cmp(a) && is_Cmp(b)) {
2638                 ir_node *a_left  = get_Cmp_left(a);
2639                 ir_node *a_right = get_Cmp_left(a);
2640                 ir_node *b_left  = get_Cmp_left(b);
2641                 ir_node *b_right = get_Cmp_right(b);
2642                 if (a_left == b_left && b_left == b_right) {
2643                         dbg_info   *dbgi         = get_irn_dbg_info(n);
2644                         ir_node    *block        = get_nodes_block(n);
2645                         ir_relation a_relation   = get_Cmp_relation(a);
2646                         ir_relation b_relation   = get_Cmp_relation(b);
2647                         ir_relation new_relation = a_relation ^ b_relation;
2648                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2649                 }
2650         }
2651
2652         HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2653
2654         /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2655         if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2656                 dbg_info *dbg      = get_irn_dbg_info(n);
2657                 ir_node  *block    = get_nodes_block(n);
2658                 ir_node  *new_not  = new_rd_Not(dbg, block, b, mode);
2659                 ir_node  *new_left = get_Not_op(a);
2660                 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2661                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2662                 return n;
2663         } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2664                 dbg_info *dbg       = get_irn_dbg_info(n);
2665                 ir_node  *block     = get_nodes_block(n);
2666                 ir_node  *new_not   = new_rd_Not(dbg, block, a, mode);
2667                 ir_node  *new_right = get_Not_op(b);
2668                 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2669                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2670                 return n;
2671         }
2672
2673         /* x ^ 1...1 -> ~1 */
2674         if (is_Const(b) && is_Const_all_one(b)) {
2675                 n = new_r_Not(get_nodes_block(n), a, mode);
2676                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2677                 return n;
2678         }
2679
2680         n = transform_bitwise_distributive(n, transform_node_Eor_);
2681         if (n != oldn)
2682                 return n;
2683         n = transform_node_bitop_shift(n);
2684         if (n != oldn)
2685                 return n;
2686
2687         return n;
2688 }
2689
2690 static ir_node *transform_node_Eor(ir_node *n)
2691 {
2692         if (is_Or_Eor_Add(n)) {
2693                 dbg_info *dbgi  = get_irn_dbg_info(n);
2694                 ir_node  *block = get_nodes_block(n);
2695                 ir_node  *left  = get_Eor_left(n);
2696                 ir_node  *right = get_Eor_right(n);
2697                 ir_mode  *mode  = get_irn_mode(n);
2698                 return new_rd_Add(dbgi, block, left, right, mode);
2699         }
2700         return transform_node_Eor_(n);
2701 }
2702
2703 /**
2704  * Do the AddSub optimization, then Transform
2705  *   Constant folding on Phi
2706  *   Add(a,a)          -> Mul(a, 2)
2707  *   Add(Mul(a, x), a) -> Mul(a, x+1)
2708  * if the mode is integer or float.
2709  * Transform Add(a,-b) into Sub(a,b).
2710  * Reassociation might fold this further.
2711  */
2712 static ir_node *transform_node_Add(ir_node *n)
2713 {
2714         ir_mode *mode;
2715         ir_node *a;
2716         ir_node *b;
2717         ir_node *c;
2718         ir_node *oldn = n;
2719
2720         n = fold_constant_associativity(n, tarval_add);
2721         if (n != oldn)
2722                 return n;
2723
2724         n = transform_node_AddSub(n);
2725         if (n != oldn)
2726                 return n;
2727
2728         a    = get_Add_left(n);
2729         b    = get_Add_right(n);
2730         mode = get_irn_mode(n);
2731
2732         if (mode_is_reference(mode)) {
2733                 ir_mode *lmode = get_irn_mode(a);
2734
2735                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2736                         /* an Add(a, NULL) is a hidden Conv */
2737                         dbg_info *dbg = get_irn_dbg_info(n);
2738                         return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2739                 }
2740         }
2741
2742         if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2743                 ir_tarval *tv  = get_Const_tarval(b);
2744                 ir_tarval *min = get_mode_min(mode);
2745                 /* if all bits are set, then this has the same effect as a Not.
2746                  * Note that the following == gives false for different modes which
2747                  * is exactly what we want */
2748                 if (tv == min) {
2749                         dbg_info *dbgi  = get_irn_dbg_info(n);
2750                         ir_graph *irg   = get_irn_irg(n);
2751                         ir_node  *block = get_nodes_block(n);
2752                         ir_node  *cnst  = new_r_Const(irg, min);
2753                         return new_rd_Eor(dbgi, block, a, cnst, mode);
2754                 }
2755         }
2756
2757         HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2758
2759         /* for FP the following optimizations are only allowed if
2760          * fp_strict_algebraic is disabled */
2761         if (mode_is_float(mode)) {
2762                 ir_graph *irg = get_irn_irg(n);
2763                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2764                         return n;
2765         }
2766
2767         if (mode_is_num(mode)) {
2768                 ir_graph *irg = get_irn_irg(n);
2769                 /* the following code leads to endless recursion when Mul are replaced
2770                  * by a simple instruction chain */
2771                 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2772                                 && a == b && mode_is_int(mode)) {
2773                         ir_node *block = get_nodes_block(n);
2774
2775                         n = new_rd_Mul(
2776                                 get_irn_dbg_info(n),
2777                                 block,
2778                                 a,
2779                                 new_r_Const_long(irg, mode, 2),
2780                                 mode);
2781                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2782                         return n;
2783                 }
2784                 if (is_Minus(a)) {
2785                         n = new_rd_Sub(
2786                                         get_irn_dbg_info(n),
2787                                         get_nodes_block(n),
2788                                         b,
2789                                         get_Minus_op(a),
2790                                         mode);
2791                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2792                         return n;
2793                 }
2794                 if (is_Minus(b)) {
2795                         n = new_rd_Sub(
2796                                         get_irn_dbg_info(n),
2797                                         get_nodes_block(n),
2798                                         a,
2799                                         get_Minus_op(b),
2800                                         mode);
2801                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2802                         return n;
2803                 }
2804                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2805                         /* Here we rely on constants be on the RIGHT side */
2806                         if (is_Not(a)) {
2807                                 ir_node *op = get_Not_op(a);
2808
2809                                 if (is_Const(b) && is_Const_one(b)) {
2810                                         /* ~x + 1 = -x */
2811                                         ir_node *blk = get_nodes_block(n);
2812                                         n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2813                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2814                                         return n;
2815                                 }
2816                         }
2817                 }
2818         }
2819
2820         if (is_Or_Eor_Add(n)) {
2821                 n = transform_node_Or_(n);
2822                 if (n != oldn)
2823                         return n;
2824                 n = transform_node_Eor_(n);
2825                 if (n != oldn)
2826                         return n;
2827         }
2828
2829         return n;
2830 }
2831
2832 /**
2833  * returns -cnst or NULL if impossible
2834  */
2835 static ir_node *const_negate(ir_node *cnst)
2836 {
2837         ir_tarval *tv    = tarval_neg(get_Const_tarval(cnst));
2838         dbg_info  *dbgi  = get_irn_dbg_info(cnst);
2839         ir_graph  *irg   = get_irn_irg(cnst);
2840         if (tv == tarval_bad) return NULL;
2841         return new_rd_Const(dbgi, irg, tv);
2842 }
2843
2844 /**
2845  * Do the AddSub optimization, then Transform
2846  *   Constant folding on Phi
2847  *   Sub(0,a)          -> Minus(a)
2848  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2849  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2850  *   Sub(Add(a, x), x) -> a
2851  *   Sub(x, Add(x, a)) -> -a
2852  *   Sub(x, Const)     -> Add(x, -Const)
2853  */
2854 static ir_node *transform_node_Sub(ir_node *n)
2855 {
2856         ir_mode *mode;
2857         ir_node *oldn = n;
2858         ir_node *a, *b, *c;
2859
2860         n = transform_node_AddSub(n);
2861
2862         a = get_Sub_left(n);
2863         b = get_Sub_right(n);
2864
2865         mode = get_irn_mode(n);
2866
2867         if (mode_is_int(mode)) {
2868                 ir_mode *lmode = get_irn_mode(a);
2869
2870                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2871                         /* a Sub(a, NULL) is a hidden Conv */
2872                         dbg_info *dbg = get_irn_dbg_info(n);
2873                         n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2874                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2875                         return n;
2876                 }
2877
2878                 if (mode == lmode                                     &&
2879                     get_mode_arithmetic(mode) == irma_twos_complement &&
2880                     is_Const(a)                                       &&
2881                     get_Const_tarval(a) == get_mode_minus_one(mode)) {
2882                         /* -1 - x -> ~x */
2883                         dbg_info *dbg = get_irn_dbg_info(n);
2884                         n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2885                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2886                         return n;
2887                 }
2888         }
2889
2890 restart:
2891         HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2892
2893         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2894         if (mode_is_float(mode)) {
2895                 ir_graph *irg = get_irn_irg(n);
2896                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2897                         return n;
2898         }
2899
2900         if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2901                 /* a - C -> a + (-C) */
2902                 ir_node *cnst = const_negate(b);
2903                 if (cnst != NULL) {
2904                         ir_node  *block = get_nodes_block(n);
2905                         dbg_info *dbgi  = get_irn_dbg_info(n);
2906
2907                         n = new_rd_Add(dbgi, block, a, cnst, mode);
2908                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2909                         return n;
2910                 }
2911         }
2912
2913         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2914                 dbg_info *dbg   = get_irn_dbg_info(n);
2915                 ir_node  *block = get_nodes_block(n);
2916                 ir_node  *left  = get_Minus_op(a);
2917                 ir_node  *add   = new_rd_Add(dbg, block, left, b, mode);
2918
2919                 n = new_rd_Minus(dbg, block, add, mode);
2920                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2921                 return n;
2922         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2923                 dbg_info *dbg   = get_irn_dbg_info(n);
2924                 ir_node  *block = get_nodes_block(n);
2925                 ir_node  *right = get_Minus_op(b);
2926
2927                 n = new_rd_Add(dbg, block, a, right, mode);
2928                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2929                 return n;
2930         } else if (is_Sub(b)) {
2931                 /* a - (b - c) -> a + (c - b)
2932                  *             -> (a - b) + c iff (b - c) is a pointer */
2933                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2934                 ir_node  *s_left  = get_Sub_left(b);
2935                 ir_node  *s_right = get_Sub_right(b);
2936                 ir_mode  *s_mode  = get_irn_mode(b);
2937                 if (mode_is_reference(s_mode)) {
2938                         ir_node  *lowest_block = get_nodes_block(n); /* a and b are live here */
2939                         ir_node  *sub     = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2940                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2941
2942                         if (s_mode != mode)
2943                                 s_right = new_r_Conv(lowest_block, s_right, mode);
2944                         n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2945                 } else {
2946                         ir_node  *s_block = get_nodes_block(b);
2947                         ir_node  *sub     = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2948                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2949                         ir_node  *a_block = get_nodes_block(n);
2950
2951                         n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2952                 }
2953                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2954                 return n;
2955 #if 0
2956         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2957                 ir_node *m_right = get_Mul_right(b);
2958                 if (is_Const(m_right)) {
2959                         ir_node *cnst2 = const_negate(m_right);
2960                         if (cnst2 != NULL) {
2961                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2962                                 ir_node  *m_block = get_nodes_block(b);
2963                                 ir_node  *m_left  = get_Mul_left(b);
2964                                 ir_mode  *m_mode  = get_irn_mode(b);
2965                                 ir_node  *mul     = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2966                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2967                                 ir_node  *a_block = get_nodes_block(n);
2968
2969                                 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2970                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2971                                 return n;
2972                         }
2973                 }
2974 #endif
2975         }
2976
2977         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2978         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2979                 n = new_rd_Minus(
2980                                 get_irn_dbg_info(n),
2981                                 get_nodes_block(n),
2982                                 b,
2983                                 mode);
2984                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2985                 return n;
2986         }
2987         if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2988                 ir_node *left  = get_binop_left(a);
2989                 ir_node *right = get_binop_right(a);
2990
2991                 /* FIXME: Does the Conv's work only for two complement or generally? */
2992                 if (left == b) {
2993                         if (mode != get_irn_mode(right)) {
2994                                 /* This Sub is an effective Cast */
2995                                 right = new_r_Conv(get_nodes_block(n), right, mode);
2996                         }
2997                         n = right;
2998                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2999                         return n;
3000                 } else if (right == b) {
3001                         if (mode != get_irn_mode(left)) {
3002                                 /* This Sub is an effective Cast */
3003                                 left = new_r_Conv(get_nodes_block(n), left, mode);
3004                         }
3005                         n = left;
3006                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3007                         return n;
3008                 }
3009         }
3010         if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
3011                 ir_node *left  = get_binop_left(b);
3012                 ir_node *right = get_binop_right(b);
3013
3014                 /* FIXME: Does the Conv's work only for two complement or generally? */
3015                 if (left == a) {
3016                         ir_mode *r_mode = get_irn_mode(right);
3017
3018                         n = new_r_Minus(get_nodes_block(n), right, r_mode);
3019                         if (mode != r_mode) {
3020                                 /* This Sub is an effective Cast */
3021                                 n = new_r_Conv(get_nodes_block(n), n, mode);
3022                         }
3023                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3024                         return n;
3025                 } else if (right == a) {
3026                         ir_mode *l_mode = get_irn_mode(left);
3027
3028                         n = new_r_Minus(get_nodes_block(n), left, l_mode);
3029                         if (mode != l_mode) {
3030                                 /* This Sub is an effective Cast */
3031                                 n = new_r_Conv(get_nodes_block(n), n, mode);
3032                         }
3033                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3034                         return n;
3035                 }
3036         }
3037         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3038                 ir_mode *mode = get_irn_mode(a);
3039
3040                 if (mode == get_irn_mode(b)) {
3041                         ir_mode *ma, *mb;
3042                         ir_node *op_a = get_Conv_op(a);
3043                         ir_node *op_b = get_Conv_op(b);
3044
3045                         /* check if it's allowed to skip the conv */
3046                         ma = get_irn_mode(op_a);
3047                         mb = get_irn_mode(op_b);
3048
3049                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
3050                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3051                                 a = op_a; b = op_b;
3052                                 set_Sub_left(n, a);
3053                                 set_Sub_right(n, b);
3054
3055                                 goto restart;
3056                         }
3057                 }
3058         }
3059         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3060         if (!is_reassoc_running() && is_Mul(a)) {
3061                 ir_node *ma = get_Mul_left(a);
3062                 ir_node *mb = get_Mul_right(a);
3063
3064                 if (ma == b) {
3065                         ir_node  *blk = get_nodes_block(n);
3066                         ir_graph *irg = get_irn_irg(n);
3067                         n = new_rd_Mul(
3068                                         get_irn_dbg_info(n),
3069                                         blk,
3070                                         ma,
3071                                         new_rd_Sub(
3072                                                 get_irn_dbg_info(n),
3073                                                 blk,
3074                                                 mb,
3075                                                 new_r_Const(irg, get_mode_one(mode)),
3076                                                 mode),
3077                                         mode);
3078                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3079                         return n;
3080                 } else if (mb == b) {
3081                         ir_node  *blk = get_nodes_block(n);
3082                         ir_graph *irg = get_irn_irg(n);
3083                         n = new_rd_Mul(
3084                                         get_irn_dbg_info(n),
3085                                         blk,
3086                                         mb,
3087                                         new_rd_Sub(
3088                                                 get_irn_dbg_info(n),
3089                                                 blk,
3090                                                 ma,
3091                                                 new_r_Const(irg, get_mode_one(mode)),
3092                                                 mode),
3093                                         mode);
3094                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3095                         return n;
3096                 }
3097         }
3098         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3099                 ir_node *x        = get_Sub_left(a);
3100                 ir_node *y        = get_Sub_right(a);
3101                 ir_node *blk      = get_nodes_block(n);
3102                 ir_mode *m_b      = get_irn_mode(b);
3103                 ir_mode *m_y      = get_irn_mode(y);
3104                 ir_mode *add_mode;
3105                 ir_node *add;
3106
3107                 /* Determine the right mode for the Add. */
3108                 if (m_b == m_y)
3109                         add_mode = m_b;
3110                 else if (mode_is_reference(m_b))
3111                         add_mode = m_b;
3112                 else if (mode_is_reference(m_y))
3113                         add_mode = m_y;
3114                 else {
3115                         /*
3116                          * Both modes are different but none is reference,
3117                          * happens for instance in SubP(SubP(P, Iu), Is).
3118                          * We have two possibilities here: Cast or ignore.
3119                          * Currently we ignore this case.
3120                          */
3121                         return n;
3122                 }
3123
3124                 add = new_r_Add(blk, y, b, add_mode);
3125
3126                 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3127                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3128                 return n;
3129         }
3130
3131         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3132                 /* c - ~X = X + (c+1) */
3133                 if (is_Const(a) && is_Not(b)) {
3134                         ir_tarval *tv = get_Const_tarval(a);
3135
3136                         tv = tarval_add(tv, get_mode_one(mode));
3137                         if (tv != tarval_bad) {
3138                                 ir_node  *blk = get_nodes_block(n);
3139                                 ir_graph *irg = get_irn_irg(n);
3140                                 ir_node *c = new_r_Const(irg, tv);
3141                                 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3142                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3143                                 return n;
3144                         }
3145                 }
3146                 /* x-(x&y) = x & ~y */
3147                 if (is_And(b)) {
3148                         ir_node *and_left  = get_And_left(b);
3149                         ir_node *and_right = get_And_right(b);
3150                         if (and_right == a) {
3151                                 ir_node *tmp = and_left;
3152                                 and_left  = and_right;
3153                                 and_right = tmp;
3154                         }
3155                         if (and_left == a) {
3156                                 dbg_info *dbgi  = get_irn_dbg_info(n);
3157                                 ir_node  *block = get_nodes_block(n);
3158                                 ir_mode  *mode  = get_irn_mode(n);
3159                                 ir_node  *notn  = new_rd_Not(dbgi, block, and_right, mode);
3160                                 ir_node  *and   = new_rd_And(dbgi, block, a, notn, mode);
3161                                 return and;
3162                         }
3163                 }
3164         }
3165         return n;
3166 }
3167
3168 /**
3169  * Several transformation done on n*n=2n bits mul.
3170  * These transformations must be done here because new nodes may be produced.
3171  */
3172 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3173 {
3174         ir_node   *oldn  = n;
3175         ir_node   *a     = get_Mul_left(n);
3176         ir_node   *b     = get_Mul_right(n);
3177         ir_tarval *ta    = value_of(a);
3178         ir_tarval *tb    = value_of(b);
3179         ir_mode   *smode = get_irn_mode(a);
3180
3181         if (ta == get_mode_one(smode)) {
3182                 /* (L)1 * (L)b = (L)b */
3183                 ir_node *blk = get_nodes_block(n);
3184                 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3185                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3186                 return n;
3187         }
3188         else if (ta == get_mode_minus_one(smode)) {
3189                 /* (L)-1 * (L)b = (L)b */
3190                 ir_node *blk = get_nodes_block(n);
3191                 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3192                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3193                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3194                 return n;
3195         }
3196         if (tb == get_mode_one(smode)) {
3197                 /* (L)a * (L)1 = (L)a */
3198                 ir_node *blk = get_irn_n(a, -1);
3199                 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3200                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3201                 return n;
3202         }
3203         else if (tb == get_mode_minus_one(smode)) {
3204                 /* (L)a * (L)-1 = (L)-a */
3205                 ir_node *blk = get_nodes_block(n);
3206                 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3207                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3208                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3209                 return n;
3210         }
3211         return n;
3212 }
3213
3214 /**
3215  * Transform Mul(a,-1) into -a.
3216  * Do constant evaluation of Phi nodes.
3217  * Do architecture dependent optimizations on Mul nodes
3218  */
3219 static ir_node *transform_node_Mul(ir_node *n)
3220 {
3221         ir_node *c, *oldn = n;
3222         ir_mode *mode = get_irn_mode(n);
3223         ir_node *a = get_Mul_left(n);
3224         ir_node *b = get_Mul_right(n);
3225
3226         n = fold_constant_associativity(n, tarval_mul);
3227         if (n != oldn)
3228                 return n;
3229
3230         if (mode != get_irn_mode(a))
3231                 return transform_node_Mul2n(n, mode);
3232
3233         HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3234
3235         if (mode_is_signed(mode)) {
3236                 ir_node *r = NULL;
3237
3238                 if (value_of(a) == get_mode_minus_one(mode))
3239                         r = b;
3240                 else if (value_of(b) == get_mode_minus_one(mode))
3241                         r = a;
3242                 if (r) {
3243                         n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3244                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3245                         return n;
3246                 }
3247         }
3248         if (is_Minus(a)) {
3249                 if (is_Const(b)) { /* (-a) * const -> a * -const */
3250                         ir_node *cnst = const_negate(b);
3251                         if (cnst != NULL) {
3252                                 dbg_info *dbgi  = get_irn_dbg_info(n);
3253                                 ir_node  *block = get_nodes_block(n);
3254                                 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3255                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3256                                 return n;
3257                         }
3258                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3259                         dbg_info *dbgi  = get_irn_dbg_info(n);
3260                         ir_node  *block = get_nodes_block(n);
3261                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3262                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3263                         return n;
3264                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3265                         ir_node  *sub_l = get_Sub_left(b);
3266                         ir_node  *sub_r = get_Sub_right(b);
3267                         dbg_info *dbgi  = get_irn_dbg_info(n);
3268                         ir_node  *block = get_nodes_block(n);
3269                         ir_node  *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3270                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3271                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3272                         return n;
3273                 }
3274         } else if (is_Minus(b)) {
3275                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3276                         ir_node  *sub_l = get_Sub_left(a);
3277                         ir_node  *sub_r = get_Sub_right(a);
3278                         dbg_info *dbgi  = get_irn_dbg_info(n);
3279                         ir_node  *block = get_nodes_block(n);
3280                         ir_node  *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3281                         n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3282                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3283                         return n;
3284                 }
3285         } else if (is_Shl(a)) {
3286                 ir_node *const shl_l = get_Shl_left(a);
3287                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3288                         /* (1 << x) * b -> b << x */
3289                         dbg_info *const dbgi  = get_irn_dbg_info(n);
3290                         ir_node  *const block = get_nodes_block(n);
3291                         ir_node  *const shl_r = get_Shl_right(a);
3292                         n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3293                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3294                         return n;
3295                 }
3296         } else if (is_Shl(b)) {
3297                 ir_node *const shl_l = get_Shl_left(b);
3298                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3299                         /* a * (1 << x) -> a << x */
3300                         dbg_info *const dbgi  = get_irn_dbg_info(n);
3301                         ir_node  *const block = get_nodes_block(n);
3302                         ir_node  *const shl_r = get_Shl_right(b);
3303                         n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3304                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3305                         return n;
3306                 }
3307         }
3308         if (get_mode_arithmetic(mode) == irma_ieee754
3309             || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3310                 if (is_Const(a)) {
3311                         ir_tarval *tv = get_Const_tarval(a);
3312                         if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3313                                         && !tarval_is_negative(tv)) {
3314                                 /* 2.0 * b = b + b */
3315                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3316                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3317                                 return n;
3318                         }
3319                 }
3320                 else if (is_Const(b)) {
3321                         ir_tarval *tv = get_Const_tarval(b);
3322                         if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3323                                         && !tarval_is_negative(tv)) {
3324                                 /* a * 2.0 = a + a */
3325                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3326                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3327                                 return n;
3328                         }
3329                 }
3330         }
3331         return arch_dep_replace_mul_with_shifts(n);
3332 }
3333
3334 /**
3335  * Transform a Div Node.
3336  */
3337 static ir_node *transform_node_Div(ir_node *n)
3338 {
3339         ir_mode *mode = get_Div_resmode(n);
3340         ir_node *a = get_Div_left(n);
3341         ir_node *b = get_Div_right(n);
3342         ir_node *value = n;
3343         const ir_node *dummy;
3344
3345         if (mode_is_int(mode)) {
3346                 if (is_Const(b) && is_const_Phi(a)) {
3347                         /* check for Div(Phi, Const) */
3348                         value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3349                         if (value) {
3350                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3351                                 goto make_tuple;
3352                         }
3353                 } else if (is_Const(a) && is_const_Phi(b)) {
3354                         /* check for Div(Const, Phi) */
3355                         value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3356                         if (value) {
3357                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3358                                 goto make_tuple;
3359                         }
3360                 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3361                         /* check for Div(Phi, Phi) */
3362                         value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3363                         if (value) {
3364                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3365                                 goto make_tuple;
3366                         }
3367                 }
3368
3369                 if (a == b && value_not_zero(a, &dummy)) {
3370                         ir_graph *irg = get_irn_irg(n);
3371                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3372                         value = new_r_Const(irg, get_mode_one(mode));
3373                         DBG_OPT_CSTEVAL(n, value);
3374                         goto make_tuple;
3375                 } else {
3376                         if (mode_is_signed(mode) && is_Const(b)) {
3377                                 ir_tarval *tv = get_Const_tarval(b);
3378
3379                                 if (tv == get_mode_minus_one(mode)) {
3380                                         /* a / -1 */
3381                                         value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3382                                         DBG_OPT_CSTEVAL(n, value);
3383                                         goto make_tuple;
3384                                 }
3385                         }
3386                         /* Try architecture dependent optimization */
3387                         value = arch_dep_replace_div_by_const(n);
3388                 }
3389         } else {
3390                 assert(mode_is_float(mode));
3391
3392                 /* Optimize x/c to x*(1/c) */
3393                 if (get_mode_arithmetic(mode) == irma_ieee754) {
3394                         ir_tarval *tv = value_of(b);
3395
3396                         if (tv != tarval_bad) {
3397                                 int rem = tarval_fp_ops_enabled();
3398
3399                                 /*
3400                                  * Floating point constant folding might be disabled here to
3401                                  * prevent rounding.
3402                                  * However, as we check for exact result, doing it is safe.
3403                                  * Switch it on.
3404                                  */
3405                                 tarval_enable_fp_ops(1);
3406                                 tv = tarval_div(get_mode_one(mode), tv);
3407                                 tarval_enable_fp_ops(rem);
3408
3409                                 /* Do the transformation if the result is either exact or we are
3410                                    not using strict rules. */
3411                                 if (tv != tarval_bad &&
3412                                         (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3413                                         ir_node  *block = get_nodes_block(n);
3414                                         ir_graph *irg   = get_irn_irg(block);
3415                                         ir_node  *c     = new_r_Const(irg, tv);
3416                                         dbg_info *dbgi  = get_irn_dbg_info(n);
3417                                         value = new_rd_Mul(dbgi, block, a, c, mode);
3418
3419                                         goto make_tuple;
3420                                 }
3421                         }
3422                 }
3423         }
3424
3425         if (value != n) {
3426                 ir_node *mem, *blk;
3427                 ir_graph *irg;
3428
3429 make_tuple:
3430                 /* Turn Div into a tuple (mem, jmp, bad, value) */
3431                 mem = get_Div_mem(n);
3432                 blk = get_nodes_block(n);
3433                 irg = get_irn_irg(blk);
3434
3435                 /* skip a potential Pin */
3436                 mem = skip_Pin(mem);
3437                 turn_into_tuple(n, pn_Div_max+1);
3438                 set_Tuple_pred(n, pn_Div_M,         mem);
3439                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3440                 set_Tuple_pred(n, pn_Div_X_except,  new_r_Bad(irg, mode_X));
3441                 set_Tuple_pred(n, pn_Div_res,       value);
3442         }
3443         return n;
3444 }
3445
3446 /**
3447  * Transform a Mod node.
3448  */
3449 static ir_node *transform_node_Mod(ir_node *n)
3450 {
3451         ir_mode   *mode = get_Mod_resmode(n);
3452         ir_node   *a    = get_Mod_left(n);
3453         ir_node   *b    = get_Mod_right(n);
3454         ir_graph  *irg;
3455         ir_node   *value;
3456         ir_tarval *tv;
3457
3458         if (is_Const(b) && is_const_Phi(a)) {
3459                 /* check for Div(Phi, Const) */
3460                 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3461                 if (value) {
3462                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3463                         goto make_tuple;
3464                 }
3465         }
3466         else if (is_Const(a) && is_const_Phi(b)) {
3467                 /* check for Div(Const, Phi) */
3468                 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3469                 if (value) {
3470                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3471                         goto make_tuple;
3472                 }
3473         }
3474         else if (is_const_Phi(a) && is_const_Phi(b)) {
3475                 /* check for Div(Phi, Phi) */
3476                 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3477                 if (value) {
3478                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3479                         goto make_tuple;
3480                 }
3481         }
3482
3483         value = n;
3484         tv = value_of(n);
3485         irg = get_irn_irg(n);
3486         if (tv != tarval_bad) {
3487                 value = new_r_Const(irg, tv);
3488
3489                 DBG_OPT_CSTEVAL(n, value);
3490                 goto make_tuple;
3491         } else {
3492                 ir_node       *a = get_Mod_left(n);
3493                 ir_node       *b = get_Mod_right(n);
3494                 const ir_node *dummy;
3495
3496                 if (a == b && value_not_zero(a, &dummy)) {
3497                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3498                         value = new_r_Const(irg, get_mode_null(mode));
3499                         DBG_OPT_CSTEVAL(n, value);
3500                         goto make_tuple;
3501                 } else {
3502                         if (mode_is_signed(mode) && is_Const(b)) {
3503                                 ir_tarval *tv = get_Const_tarval(b);
3504
3505                                 if (tv == get_mode_minus_one(mode)) {
3506                                         /* a % -1 = 0 */
3507                                         value = new_r_Const(irg, get_mode_null(mode));
3508                                         DBG_OPT_CSTEVAL(n, value);
3509                                         goto make_tuple;
3510                                 }
3511                         }
3512                         /* Try architecture dependent optimization */
3513                         value = arch_dep_replace_mod_by_const(n);
3514                 }
3515         }
3516
3517         if (value != n) {
3518                 ir_node *mem, *blk;
3519                 ir_graph *irg;
3520
3521 make_tuple:
3522                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3523                 mem = get_Mod_mem(n);
3524                 blk = get_nodes_block(n);
3525                 irg = get_irn_irg(blk);
3526
3527                 /* skip a potential Pin */
3528                 mem = skip_Pin(mem);
3529                 turn_into_tuple(n, pn_Mod_max+1);
3530                 set_Tuple_pred(n, pn_Mod_M,         mem);
3531                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3532                 set_Tuple_pred(n, pn_Mod_X_except,  new_r_Bad(irg, mode_X));
3533                 set_Tuple_pred(n, pn_Mod_res,       value);
3534         }
3535         return n;
3536 }
3537
3538 /**
3539  * Transform a Cond node.
3540  *
3541  * Replace the Cond by a Jmp if it branches on a constant
3542  * condition.
3543  */
3544 static ir_node *transform_node_Cond(ir_node *n)
3545 {
3546         ir_node   *a   = get_Cond_selector(n);
3547         ir_graph  *irg = get_irn_irg(n);
3548         ir_tarval *ta;
3549         ir_node   *jmp;
3550
3551         /* we need block info which is not available in floating irgs */
3552         if (get_irg_pinned(irg) == op_pin_state_floats)
3553                 return n;
3554
3555         ta = value_of(a);
3556         if (ta == tarval_bad && is_Cmp(a)) {
3557                 /* try again with a direct call to compute_cmp, as we don't care
3558                  * about the MODEB_LOWERED flag here */
3559                 ta = compute_cmp(a);
3560         }
3561
3562         if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3563                 /* It's a boolean Cond, branching on a boolean constant.
3564                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3565                 ir_node *blk = get_nodes_block(n);
3566                 jmp = new_r_Jmp(blk);
3567                 turn_into_tuple(n, pn_Cond_max+1);
3568                 if (ta == tarval_b_true) {
3569                         set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3570                         set_Tuple_pred(n, pn_Cond_true, jmp);
3571                 } else {
3572                         set_Tuple_pred(n, pn_Cond_false, jmp);
3573                         set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3574                 }
3575                 /* We might generate an endless loop, so keep it alive. */
3576                 add_End_keepalive(get_irg_end(irg), blk);
3577                 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3578         }
3579         return n;
3580 }
3581
3582 static ir_node *transform_node_Switch(ir_node *n)
3583 {
3584         ir_node   *op  = get_Switch_selector(n);
3585         ir_tarval *val = value_of(op);
3586         if (val != tarval_bad) {
3587                 dbg_info              *dbgi      = get_irn_dbg_info(n);
3588                 ir_graph              *irg       = get_irn_irg(n);
3589                 unsigned               n_outs    = get_Switch_n_outs(n);
3590                 ir_node               *block     = get_nodes_block(n);
3591                 ir_node               *bad       = new_r_Bad(irg, mode_X);
3592                 ir_node              **in        = XMALLOCN(ir_node*, n_outs);
3593                 const ir_switch_table *table     = get_Switch_table(n);
3594                 size_t                 n_entries = ir_switch_table_get_n_entries(table);
3595                 long                   jmp_pn    = 0;
3596                 size_t                 i;
3597                 unsigned               o;
3598                 for (i = 0; i < n_entries; ++i) {
3599                         const ir_switch_table_entry *entry
3600                                 = ir_switch_table_get_entry_const(table, i);
3601                         ir_tarval *min = entry->min;
3602                         ir_tarval *max = entry->max;
3603                         if (entry->pn == 0)
3604                                 continue;
3605                         if ((min == max && min == val)
3606                             || (tarval_cmp(val, min) != ir_relation_less
3607                                 && tarval_cmp(val, max) != ir_relation_greater)) {
3608                             jmp_pn = entry->pn;
3609                             break;
3610                         }
3611                 }
3612                 for (o = 0; o < n_outs; ++o) {
3613                         if (o == (unsigned)jmp_pn) {
3614                                 in[o] = new_rd_Jmp(dbgi, block);
3615                         } else {
3616                                 in[o] = bad;
3617                         }
3618                 }
3619                 return new_r_Tuple(block, (int)n_outs, in);
3620         }
3621         return n;
3622 }
3623
3624 /**
3625  * normalisation: (x & c1) >> c2   to   (x >> c2) & (c1 >> c2)
3626  *  (we can use:
3627  *    - and, or, xor          instead of &
3628  *    - Shl, Shr, Shrs, rotl  instead of >>
3629  *    (with a special case for Or/Xor + Shrs)
3630  *
3631  * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3632  */
3633 static ir_node *transform_node_shift_bitop(ir_node *n)
3634 {
3635         ir_graph  *irg   = get_irn_irg(n);
3636         ir_node   *right = get_binop_right(n);
3637         ir_mode   *mode  = get_irn_mode(n);
3638         ir_node   *left;
3639         ir_node   *bitop_left;
3640         ir_node   *bitop_right;
3641         ir_op     *op_left;
3642         ir_node   *block;
3643         dbg_info  *dbgi;
3644         ir_node   *new_shift;
3645         ir_node   *new_bitop;
3646         ir_node   *new_const;
3647         ir_tarval *tv1;
3648         ir_tarval *tv2;
3649         ir_tarval *tv_shift;
3650
3651         if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3652                 return n;
3653
3654         assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3655
3656         if (!is_Const(right))
3657                 return n;
3658
3659         left    = get_binop_left(n);
3660         op_left = get_irn_op(left);
3661         if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3662                 return n;
3663
3664         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3665         if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3666                 /* TODO: test if sign bit is affectes */
3667                 return n;
3668         }
3669
3670         bitop_right = get_binop_right(left);
3671         if (!is_Const(bitop_right))
3672                 return n;
3673
3674         bitop_left = get_binop_left(left);
3675
3676         block = get_nodes_block(n);
3677         dbgi  = get_irn_dbg_info(n);
3678         tv1   = get_Const_tarval(bitop_right);
3679         tv2   = get_Const_tarval(right);
3680
3681         assert(get_tarval_mode(tv1) == mode);
3682
3683         if (is_Shl(n)) {
3684                 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3685                 tv_shift  = tarval_shl(tv1, tv2);
3686         } else if (is_Shr(n)) {
3687                 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3688                 tv_shift  = tarval_shr(tv1, tv2);
3689         } else if (is_Shrs(n)) {
3690                 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3691                 tv_shift  = tarval_shrs(tv1, tv2);
3692         } else {
3693                 assert(is_Rotl(n));
3694                 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3695                 tv_shift  = tarval_rotl(tv1, tv2);
3696         }
3697
3698         assert(get_tarval_mode(tv_shift) == mode);
3699         irg       = get_irn_irg(n);
3700         new_const = new_r_Const(irg, tv_shift);
3701
3702         if (op_left == op_And) {
3703                 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3704         } else if (op_left == op_Or) {
3705                 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3706         } else {
3707                 assert(op_left == op_Eor);
3708                 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3709         }
3710
3711         return new_bitop;
3712 }
3713
3714 /**
3715  * Transform an And.
3716  */
3717 static ir_node *transform_node_And(ir_node *n)
3718 {
3719         ir_node *c, *oldn = n;
3720         ir_node *a = get_And_left(n);
3721         ir_node *b = get_And_right(n);
3722         ir_mode *mode;
3723
3724         n = fold_constant_associativity(n, tarval_and);
3725         if (n != oldn)
3726                 return n;
3727
3728         if (is_Cmp(a) && is_Cmp(b)) {
3729                 ir_node    *a_left     = get_Cmp_left(a);
3730                 ir_node    *a_right    = get_Cmp_right(a);
3731                 ir_node    *b_left     = get_Cmp_left(b);
3732                 ir_node    *b_right    = get_Cmp_right(b);
3733                 ir_relation a_relation = get_Cmp_relation(a);
3734                 ir_relation b_relation = get_Cmp_relation(b);
3735                 /* we can combine the relations of two compares with the same
3736                  * operands */
3737                 if (a_left == b_left && b_left == b_right) {
3738                         dbg_info   *dbgi         = get_irn_dbg_info(n);
3739                         ir_node    *block        = get_nodes_block(n);
3740                         ir_relation new_relation = a_relation & b_relation;
3741                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3742                 }
3743                 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3744                 if (a_relation == b_relation && a_relation == ir_relation_equal
3745                     && !mode_is_float(get_irn_mode(a_left))
3746                     && !mode_is_float(get_irn_mode(b_left))) {
3747                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3748                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3749                                 ir_node  *block  = get_nodes_block(n);
3750                                 ir_mode  *a_mode = get_irn_mode(a_left);
3751                                 ir_mode  *b_mode = get_irn_mode(b_left);
3752                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3753                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3754                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
3755                                 ir_node  *or     = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3756                                 ir_graph *irg    = get_irn_irg(n);
3757                                 ir_node  *zero   = create_zero_const(irg, b_mode);
3758                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3759                         }
3760                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3761                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3762                                 ir_node  *block  = get_nodes_block(n);
3763                                 ir_mode  *a_mode = get_irn_mode(a_left);
3764                                 ir_mode  *b_mode = get_irn_mode(b_left);
3765                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3766                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3767                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
3768                                 ir_node  *or     = new_rd_Or(dbgi, block, xora, conv, a_mode);
3769                                 ir_graph *irg    = get_irn_irg(n);
3770                                 ir_node  *zero   = create_zero_const(irg, a_mode);
3771                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3772                         }
3773                 }
3774         }
3775
3776         mode = get_irn_mode(n);
3777         HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3778
3779         if (is_Or(a) || is_Or_Eor_Add(a)) {
3780                 ir_node *or_left  = get_binop_left(a);
3781                 ir_node *or_right = get_binop_right(a);
3782                 if (complement_values(or_left, b)) {
3783                         /* (a|b) & ~a => b & ~a */
3784                         dbg_info *dbgi    = get_irn_dbg_info(n);
3785                         ir_node  *block   = get_nodes_block(n);
3786                         return new_rd_And(dbgi, block, or_right, b, mode);
3787                 } else if (complement_values(or_right, b)) {
3788                         /* (a|b) & ~b => a & ~b */
3789                         dbg_info *dbgi    = get_irn_dbg_info(n);
3790                         ir_node  *block   = get_nodes_block(n);
3791                         return new_rd_And(dbgi, block, or_left, b, mode);
3792                 } else if (is_Not(b)) {
3793                         ir_node *op = get_Not_op(b);
3794                         if (is_And(op)) {
3795                                 ir_node *ba = get_And_left(op);
3796                                 ir_node *bb = get_And_right(op);
3797
3798                                 /* it's enough to test the following cases due to normalization! */
3799                                 if (or_left == ba && or_right == bb) {
3800                                         /* (a|b) & ~(a&b) = a^b */
3801                                         ir_node *block = get_nodes_block(n);
3802
3803                                         n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3804                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3805                                         return n;
3806                                 }
3807                         }
3808                 }
3809         }
3810         if (is_Or(b) || is_Or_Eor_Add(b)) {
3811                 ir_node *or_left  = get_binop_left(b);
3812                 ir_node *or_right = get_binop_right(b);
3813                 if (complement_values(or_left, a)) {
3814                         /* (a|b) & ~a => b & ~a */
3815                         dbg_info *dbgi    = get_irn_dbg_info(n);
3816                         ir_node  *block   = get_nodes_block(n);
3817                         return new_rd_And(dbgi, block, or_right, a, mode);
3818                 } else if (complement_values(or_right, a)) {
3819                         /* (a|b) & ~b => a & ~b */
3820                         dbg_info *dbgi    = get_irn_dbg_info(n);
3821                         ir_node  *block   = get_nodes_block(n);
3822                         return new_rd_And(dbgi, block, or_left, a, mode);
3823                 } else if (is_Not(a)) {
3824                         ir_node *op = get_Not_op(a);
3825                         if (is_And(op)) {
3826                                 ir_node *aa = get_And_left(op);
3827                                 ir_node *ab = get_And_right(op);
3828
3829                                 /* it's enough to test the following cases due to normalization! */
3830                                 if (or_left == aa && or_right == ab) {
3831                                         /* (a|b) & ~(a&b) = a^b */
3832                                         ir_node *block = get_nodes_block(n);
3833
3834                                         n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3835                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3836                                         return n;
3837                                 }
3838                         }
3839                 }
3840         }
3841         if (is_Eor(a) || is_Or_Eor_Add(a)) {
3842                 ir_node *al = get_binop_left(a);
3843                 ir_node *ar = get_binop_right(a);
3844
3845                 if (al == b) {
3846                         /* (b ^ a) & b -> ~a & b */
3847                         dbg_info *dbg  = get_irn_dbg_info(n);
3848                         ir_node *block = get_nodes_block(n);
3849
3850                         ar = new_rd_Not(dbg, block, ar, mode);
3851                         n  = new_rd_And(dbg, block, ar, b, mode);
3852                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3853                         return n;
3854                 }
3855                 if (ar == b) {
3856                         /* (a ^ b) & b -> ~a & b */
3857                         dbg_info *dbg  = get_irn_dbg_info(n);
3858                         ir_node *block = get_nodes_block(n);
3859
3860                         al = new_rd_Not(dbg, block, al, mode);
3861                         n  = new_rd_And(dbg, block, al, b, mode);
3862                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3863                         return n;
3864                 }
3865         }
3866         if (is_Eor(b) || is_Or_Eor_Add(b)) {
3867                 ir_node *bl = get_binop_left(b);
3868                 ir_node *br = get_binop_right(b);
3869
3870                 if (bl == a) {
3871                         /* a & (a ^ b) -> a & ~b */
3872                         dbg_info *dbg  = get_irn_dbg_info(n);
3873                         ir_node *block = get_nodes_block(n);
3874
3875                         br = new_rd_Not(dbg, block, br, mode);
3876                         n  = new_rd_And(dbg, block, br, a, mode);
3877                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3878                         return n;
3879                 }
3880                 if (br == a) {
3881                         /* a & (b ^ a) -> a & ~b */
3882                         dbg_info *dbg  = get_irn_dbg_info(n);
3883                         ir_node *block = get_nodes_block(n);
3884
3885                         bl = new_rd_Not(dbg, block, bl, mode);
3886                         n  = new_rd_And(dbg, block, bl, a, mode);
3887                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3888                         return n;
3889                 }
3890         }
3891         if (is_Not(a) && is_Not(b)) {
3892                 /* ~a & ~b = ~(a|b) */
3893                 ir_node *block = get_nodes_block(n);
3894                 ir_mode *mode = get_irn_mode(n);
3895
3896                 a = get_Not_op(a);
3897                 b = get_Not_op(b);
3898                 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3899                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3900                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3901                 return n;
3902         }
3903
3904         if (is_Const(a)) {
3905                 vrp_attr  *b_vrp = vrp_get_info(b);
3906                 ir_tarval *a_val = get_Const_tarval(a);
3907                 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3908                         return b;
3909                 }
3910         }
3911
3912         if (is_Const(b)) {
3913                 vrp_attr  *a_vrp = vrp_get_info(a);
3914                 ir_tarval *b_val = get_Const_tarval(b);
3915                 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3916                         return a;
3917                 }
3918         }
3919
3920         n = transform_bitwise_distributive(n, transform_node_And);
3921         if (is_And(n))
3922                 n = transform_node_bitop_shift(n);
3923
3924         return n;
3925 }
3926
3927 /**
3928  * Transform a Not.
3929  */
3930 static ir_node *transform_node_Not(ir_node *n)
3931 {
3932         ir_node *c, *oldn = n;
3933         ir_node *a    = get_Not_op(n);
3934         ir_mode *mode = get_irn_mode(n);
3935
3936         HANDLE_UNOP_PHI(tarval_not,a,c);
3937
3938         /* check for a boolean Not */
3939         if (is_Cmp(a)) {
3940                 dbg_info *dbgi  = get_irn_dbg_info(a);
3941                 ir_node  *block = get_nodes_block(a);
3942                 ir_relation relation = get_Cmp_relation(a);
3943                 relation = get_negated_relation(relation);
3944                 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3945                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3946                 return n;
3947         }
3948
3949         /* normalize ~(a ^ b) => a ^ ~b */
3950         if (is_Eor(a) || is_Or_Eor_Add(a)) {
3951                 dbg_info *dbg       = get_irn_dbg_info(n);
3952                 ir_node  *block     = get_nodes_block(n);
3953                 ir_node  *eor_right = get_binop_right(a);
3954                 ir_node  *eor_left  = get_binop_left(a);
3955                 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3956                 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3957                 return n;
3958         }
3959
3960         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3961                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3962                         dbg_info *dbg   = get_irn_dbg_info(n);
3963                         ir_graph *irg   = get_irn_irg(n);
3964                         ir_node  *block = get_nodes_block(n);
3965                         ir_node  *add_l = get_Minus_op(a);
3966                         ir_node  *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3967                         n = new_rd_Add(dbg, block, add_l, add_r, mode);
3968                 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3969                         ir_node *add_r = get_binop_right(a);
3970                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3971                                 /* ~(x + -1) = -x */
3972                                 ir_node *op  = get_binop_left(a);
3973                                 ir_node *blk = get_nodes_block(n);
3974                                 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3975                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3976                         }
3977                 }
3978         }
3979         return n;
3980 }
3981
3982 /**
3983  * Transform a Minus.
3984  * Optimize:
3985  *   -(~x) = x + 1
3986  *   -(a-b) = b - a
3987  *   -(a >>u (size-1)) = a >>s (size-1)
3988  *   -(a >>s (size-1)) = a >>u (size-1)
3989  *   -(a * const) -> a * -const
3990  */
3991 static ir_node *transform_node_Minus(ir_node *n)
3992 {
3993         ir_node *c, *oldn = n;
3994         ir_node *a = get_Minus_op(n);
3995         ir_mode *mode;
3996
3997         HANDLE_UNOP_PHI(tarval_neg,a,c);
3998
3999         mode = get_irn_mode(a);
4000         if (get_mode_arithmetic(mode) == irma_twos_complement) {
4001                 /* the following rules are only to twos-complement */
4002                 if (is_Not(a)) {
4003                         /* -(~x) = x + 1 */
4004                         ir_node   *op  = get_Not_op(a);
4005                         ir_tarval *tv  = get_mode_one(mode);
4006                         ir_node   *blk = get_nodes_block(n);
4007                         ir_graph  *irg = get_irn_irg(blk);
4008                         ir_node   *c   = new_r_Const(irg, tv);
4009                         n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
4010                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4011                         return n;
4012                 }
4013                 if (is_Shr(a)) {
4014                         ir_node *c = get_Shr_right(a);
4015
4016                         if (is_Const(c)) {
4017                                 ir_tarval *tv = get_Const_tarval(c);
4018
4019                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4020                                         /* -(a >>u (size-1)) = a >>s (size-1) */
4021                                         ir_node *v = get_Shr_left(a);
4022
4023                                         n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4024                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4025                                         return n;
4026                                 }
4027                         }
4028                 }
4029                 if (is_Shrs(a)) {
4030                         ir_node *c = get_Shrs_right(a);
4031
4032                         if (is_Const(c)) {
4033                                 ir_tarval *tv = get_Const_tarval(c);
4034
4035                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4036                                         /* -(a >>s (size-1)) = a >>u (size-1) */
4037                                         ir_node *v = get_Shrs_left(a);
4038
4039                                         n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4040                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4041                                         return n;
4042                                 }
4043                         }
4044                 }
4045         }
4046         if (is_Sub(a)) {
4047                 /* - (a-b) = b - a */
4048                 ir_node *la  = get_Sub_left(a);
4049                 ir_node *ra  = get_Sub_right(a);
4050                 ir_node *blk = get_nodes_block(n);
4051
4052                 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4053                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4054                 return n;
4055         }
4056
4057         if (is_Mul(a)) { /* -(a * const) -> a * -const */
4058                 ir_node   *mul_l = get_Mul_left(a);
4059                 ir_node   *mul_r = get_Mul_right(a);
4060                 ir_tarval *tv    = value_of(mul_r);
4061                 if (tv != tarval_bad) {
4062                         tv = tarval_neg(tv);
4063                         if (tv != tarval_bad) {
4064                                 ir_graph *irg   = get_irn_irg(n);
4065                                 ir_node  *cnst  = new_r_Const(irg, tv);
4066                                 dbg_info *dbg   = get_irn_dbg_info(a);
4067                                 ir_node  *block = get_nodes_block(a);
4068                                 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4069                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4070                                 return n;
4071                         }
4072                 }
4073         }
4074
4075         return n;
4076 }
4077
4078 /**
4079  * Transform a Proj(Load) with a non-null address.
4080  */
4081 static ir_node *transform_node_Proj_Load(ir_node *proj)
4082 {
4083         if (get_opt_ldst_only_null_ptr_exceptions()) {
4084                 if (get_irn_mode(proj) == mode_X) {
4085                         ir_node *load = get_Proj_pred(proj);
4086
4087                         /* get the Load address */
4088                         const ir_node *addr = get_Load_ptr(load);
4089                         const ir_node *confirm;
4090
4091                         if (value_not_null(addr, &confirm)) {
4092                                 if (confirm == NULL) {
4093                                         /* this node may float if it did not depend on a Confirm */
4094                                         set_irn_pinned(load, op_pin_state_floats);
4095                                 }
4096                                 if (get_Proj_proj(proj) == pn_Load_X_except) {
4097                                         ir_graph *irg = get_irn_irg(proj);
4098                                         DBG_OPT_EXC_REM(proj);
4099                                         return new_r_Bad(irg, mode_X);
4100                                 } else {
4101                                         ir_node *blk = get_nodes_block(load);
4102                                         return new_r_Jmp(blk);
4103                                 }
4104                         }
4105                 }
4106         }
4107         return proj;
4108 }
4109
4110 /**
4111  * Transform a Proj(Store) with a non-null address.
4112  */
4113 static ir_node *transform_node_Proj_Store(ir_node *proj)
4114 {
4115         if (get_opt_ldst_only_null_ptr_exceptions()) {
4116                 if (get_irn_mode(proj) == mode_X) {
4117                         ir_node *store = get_Proj_pred(proj);
4118
4119                         /* get the load/store address */
4120                         const ir_node *addr = get_Store_ptr(store);
4121                         const ir_node *confirm;
4122
4123                         if (value_not_null(addr, &confirm)) {
4124                                 if (confirm == NULL) {
4125                                         /* this node may float if it did not depend on a Confirm */
4126                                         set_irn_pinned(store, op_pin_state_floats);
4127                                 }
4128                                 if (get_Proj_proj(proj) == pn_Store_X_except) {
4129                                         ir_graph *irg = get_irn_irg(proj);
4130                                         DBG_OPT_EXC_REM(proj);
4131                                         return new_r_Bad(irg, mode_X);
4132                                 } else {
4133                                         ir_node *blk = get_nodes_block(store);
4134                                         return new_r_Jmp(blk);
4135                                 }
4136                         }
4137                 }
4138         }
4139         return proj;
4140 }
4141
4142 /**
4143  * Transform a Proj(Div) with a non-zero value.
4144  * Removes the exceptions and routes the memory to the NoMem node.
4145  */
4146 static ir_node *transform_node_Proj_Div(ir_node *proj)
4147 {
4148         ir_node *div = get_Proj_pred(proj);
4149         ir_node *b   = get_Div_right(div);
4150         ir_node *res, *new_mem;
4151         const ir_node *confirm;
4152         long proj_nr;
4153
4154         if (value_not_zero(b, &confirm)) {
4155                 /* div(x, y) && y != 0 */
4156                 if (confirm == NULL) {
4157                         /* we are sure we have a Const != 0 */
4158                         new_mem = get_Div_mem(div);
4159                         new_mem = skip_Pin(new_mem);
4160                         set_Div_mem(div, new_mem);
4161                         set_irn_pinned(div, op_pin_state_floats);
4162                 }
4163
4164                 proj_nr = get_Proj_proj(proj);
4165                 switch (proj_nr) {
4166                 case pn_Div_X_regular:
4167                         return new_r_Jmp(get_nodes_block(div));
4168
4169                 case pn_Div_X_except: {
4170                         ir_graph *irg = get_irn_irg(proj);
4171                         /* we found an exception handler, remove it */
4172                         DBG_OPT_EXC_REM(proj);
4173                         return new_r_Bad(irg, mode_X);
4174                 }
4175
4176                 case pn_Div_M: {
4177                         ir_graph *irg = get_irn_irg(proj);
4178                         res = get_Div_mem(div);
4179                         new_mem = get_irg_no_mem(irg);
4180
4181                         if (confirm) {
4182                                 /* This node can only float up to the Confirm block */
4183                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4184                         }
4185                         set_irn_pinned(div, op_pin_state_floats);
4186                         /* this is a Div without exception, we can remove the memory edge */
4187                         set_Div_mem(div, new_mem);
4188                         return res;
4189                 }
4190                 }
4191         }
4192         return proj;
4193 }
4194
4195 /**
4196  * Transform a Proj(Mod) with a non-zero value.
4197  * Removes the exceptions and routes the memory to the NoMem node.
4198  */
4199 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4200 {
4201         ir_node *mod = get_Proj_pred(proj);
4202         ir_node *b   = get_Mod_right(mod);
4203         ir_node *res, *new_mem;
4204         const ir_node *confirm;
4205         long proj_nr;
4206
4207         if (value_not_zero(b, &confirm)) {
4208                 /* mod(x, y) && y != 0 */
4209                 proj_nr = get_Proj_proj(proj);
4210
4211                 if (confirm == NULL) {
4212                         /* we are sure we have a Const != 0 */
4213                         new_mem = get_Mod_mem(mod);
4214                         new_mem = skip_Pin(new_mem);
4215                         set_Mod_mem(mod, new_mem);
4216                         set_irn_pinned(mod, op_pin_state_floats);
4217                 }
4218
4219                 switch (proj_nr) {
4220
4221                 case pn_Mod_X_regular:
4222                         return new_r_Jmp(get_irn_n(mod, -1));
4223
4224                 case pn_Mod_X_except: {
4225                         ir_graph *irg = get_irn_irg(proj);
4226                         /* we found an exception handler, remove it */
4227                         DBG_OPT_EXC_REM(proj);
4228                         return new_r_Bad(irg, mode_X);
4229                 }
4230
4231                 case pn_Mod_M: {
4232                         ir_graph *irg = get_irn_irg(proj);
4233                         res = get_Mod_mem(mod);
4234                         new_mem = get_irg_no_mem(irg);
4235
4236                         if (confirm) {
4237                                 /* This node can only float up to the Confirm block */
4238                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4239                         }
4240                         /* this is a Mod without exception, we can remove the memory edge */
4241                         set_Mod_mem(mod, new_mem);
4242                         return res;
4243                 }
4244                 case pn_Mod_res:
4245                         if (get_Mod_left(mod) == b) {
4246                                 /* a % a = 0 if a != 0 */
4247                                 ir_graph *irg  = get_irn_irg(proj);
4248                                 ir_mode  *mode = get_irn_mode(proj);
4249                                 ir_node  *res  = new_r_Const(irg, get_mode_null(mode));
4250
4251                                 DBG_OPT_CSTEVAL(mod, res);
4252                                 return res;
4253                         }
4254                 }
4255         }
4256         return proj;
4257 }
4258
4259 /**
4260  * return true if the operation returns a value with exactly 1 bit set
4261  */
4262 static bool is_single_bit(const ir_node *node)
4263 {
4264         /* a first implementation, could be extended with vrp and others... */
4265         if (is_Shl(node)) {
4266                 ir_node *shl_l  = get_Shl_left(node);
4267                 ir_mode *mode   = get_irn_mode(node);
4268                 int      modulo = get_mode_modulo_shift(mode);
4269                 /* this works if we shift a 1 and we have modulo shift */
4270                 if (is_Const(shl_l) && is_Const_one(shl_l)
4271                                 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4272                         return true;
4273                 }
4274         } else if (is_Const(node)) {
4275                 ir_tarval *tv = get_Const_tarval(node);
4276                 return tarval_is_single_bit(tv);
4277         }
4278         return false;
4279 }
4280
4281 /**
4282  * checks if node just flips a bit in another node and returns that other node
4283  * if so. @p tv should be a value having just 1 bit set
4284  */
4285 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4286 {
4287         if (is_Not(node))
4288                 return get_Not_op(node);
4289         if (is_Eor(node)) {
4290                 ir_node *right = get_Eor_right(node);
4291                 if (is_Const(right)) {
4292                         ir_tarval *right_tv = get_Const_tarval(right);
4293                         ir_mode   *mode     = get_irn_mode(node);
4294                         if (tarval_and(right_tv, tv) != get_mode_null(mode))
4295                                 return get_Eor_left(node);
4296                 }
4297         }
4298         return NULL;
4299 }
4300
4301 /**
4302  * Normalizes and optimizes Cmp nodes.
4303  */
4304 static ir_node *transform_node_Cmp(ir_node *n)
4305 {
4306         ir_node    *left     = get_Cmp_left(n);
4307         ir_node    *right    = get_Cmp_right(n);
4308         ir_mode    *mode     = get_irn_mode(left);
4309         ir_tarval  *tv       = NULL;
4310         bool        changed  = false;
4311         bool        changedc = false;
4312         ir_relation relation = get_Cmp_relation(n);
4313         ir_relation possible = ir_get_possible_cmp_relations(left, right);
4314
4315         /* mask out impossible relations */
4316         ir_relation new_relation = relation & possible;
4317         if (new_relation != relation) {
4318                 relation = new_relation;
4319                 changed  = true;
4320         }
4321
4322         /* Remove unnecessary conversions */
4323         if (is_Conv(left) && is_Conv(right)) {
4324                 ir_node *op_left    = get_Conv_op(left);
4325                 ir_node *op_right   = get_Conv_op(right);
4326                 ir_mode *mode_left  = get_irn_mode(op_left);
4327                 ir_mode *mode_right = get_irn_mode(op_right);
4328
4329                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4330                                 && mode_left != mode_b && mode_right != mode_b) {
4331                         ir_node *block = get_nodes_block(n);
4332
4333                         if (mode_left == mode_right) {
4334                                 left    = op_left;
4335                                 right   = op_right;
4336                                 changed = true;
4337                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4338                         } else if (smaller_mode(mode_left, mode_right)) {
4339                                 left    = new_r_Conv(block, op_left, mode_right);
4340                                 right   = op_right;
4341                                 changed = true;
4342                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4343                         } else if (smaller_mode(mode_right, mode_left)) {
4344                                 left    = op_left;
4345                                 right   = new_r_Conv(block, op_right, mode_left);
4346                                 changed = true;
4347                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4348                         }
4349                         mode = get_irn_mode(left);
4350                 }
4351         }
4352         if (is_Conv(left) && is_Const(right)) {
4353                 ir_node   *op_left   = get_Conv_op(left);
4354                 ir_mode   *mode_left = get_irn_mode(op_left);
4355                 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4356                         ir_tarval *tv        = get_Const_tarval(right);
4357                         tarval_int_overflow_mode_t last_mode
4358                                 = tarval_get_integer_overflow_mode();
4359                         ir_tarval *new_tv;
4360                         tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4361                         new_tv = tarval_convert_to(tv, mode_left);
4362                         tarval_set_integer_overflow_mode(last_mode);
4363                         if (new_tv != tarval_bad) {
4364                                 ir_graph *irg = get_irn_irg(n);
4365                                 left    = op_left;
4366                                 right   = new_r_Const(irg, new_tv);
4367                                 mode    = get_irn_mode(left);
4368                                 changed = true;
4369                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4370                         }
4371                 }
4372         }
4373
4374         /*
4375          * Optimize -a CMP -b into b CMP a.
4376          * This works only for modes where unary Minus cannot Overflow.
4377          * Note that two-complement integers can Overflow so it will NOT work.
4378          */
4379         if (!mode_overflow_on_unary_Minus(mode) &&
4380                         is_Minus(left) && is_Minus(right)) {
4381                 left     = get_Minus_op(left);
4382                 right    = get_Minus_op(right);
4383                 relation = get_inversed_relation(relation);
4384                 changed  = true;
4385                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4386         }
4387
4388         /* remove operation on both sides if possible */
4389         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4390                 /*
4391                  * The following operations are NOT safe for floating point operations, for instance
4392                  * 1.0 + inf == 2.0 + inf, =/=> x == y
4393                  */
4394                 if (mode_is_int(mode)) {
4395                         unsigned lop = get_irn_opcode(left);
4396
4397                         if (lop == get_irn_opcode(right)) {
4398                                 ir_node *ll, *lr, *rl, *rr;
4399
4400                                 /* same operation on both sides, try to remove */
4401                                 switch (lop) {
4402                                 case iro_Not:
4403                                 case iro_Minus:
4404                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4405                                         left  = get_unop_op(left);
4406                                         right = get_unop_op(right);
4407                                         changed = true;
4408                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4409                                         break;
4410                                 case iro_Add:
4411                                         ll = get_Add_left(left);
4412                                         lr = get_Add_right(left);
4413                                         rl = get_Add_left(right);
4414                                         rr = get_Add_right(right);
4415
4416                                         if (ll == rl) {
4417                                                 /* X + a CMP X + b ==> a CMP b */
4418                                                 left  = lr;
4419                                                 right = rr;
4420                                                 changed = true;
4421                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4422                                         } else if (ll == rr) {
4423                                                 /* X + a CMP b + X ==> a CMP b */
4424                                                 left  = lr;
4425                                                 right = rl;
4426                                                 changed = true;
4427                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4428                                         } else if (lr == rl) {
4429                                                 /* a + X CMP X + b ==> a CMP b */
4430                                                 left  = ll;
4431                                                 right = rr;
4432                                                 changed = true;
4433                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4434                                         } else if (lr == rr) {
4435                                                 /* a + X CMP b + X ==> a CMP b */
4436                                                 left  = ll;
4437                                                 right = rl;
4438                                                 changed = true;
4439                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4440                                         }
4441                                         break;
4442                                 case iro_Sub:
4443                                         ll = get_Sub_left(left);
4444                                         lr = get_Sub_right(left);
4445                                         rl = get_Sub_left(right);
4446                                         rr = get_Sub_right(right);
4447
4448                                         if (ll == rl) {
4449                                                 /* X - a CMP X - b ==> a CMP b */
4450                                                 left  = lr;
4451                                                 right = rr;
4452                                                 changed = true;
4453                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4454                                         } else if (lr == rr) {
4455                                                 /* a - X CMP b - X ==> a CMP b */
4456                                                 left  = ll;
4457                                                 right = rl;
4458                                                 changed = true;
4459                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4460                                         }
4461                                         break;
4462                                 case iro_Rotl:
4463                                         if (get_Rotl_right(left) == get_Rotl_right(right)) {
4464                                                 /* a ROTL X CMP b ROTL X ==> a CMP b */
4465                                                 left  = get_Rotl_left(left);
4466                                                 right = get_Rotl_left(right);
4467                                                 changed = true;
4468                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4469                                         }
4470                                         break;
4471                                 default:
4472                                         break;
4473                                 }
4474                         }
4475
4476                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
4477                         if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4478                                 ir_node *ll = get_binop_left(left);
4479                                 ir_node *lr = get_binop_right(left);
4480
4481                                 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4482                                         ir_node *tmp = ll;
4483                                         ll = lr;
4484                                         lr = tmp;
4485                                 }
4486                                 if (ll == right) {
4487                                         ir_graph *irg = get_irn_irg(n);
4488                                         left     = lr;
4489                                         right   = create_zero_const(irg, mode);
4490                                         changed = true;
4491                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4492                                 }
4493                         }
4494                         if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4495                                 ir_node *rl = get_binop_left(right);
4496                                 ir_node *rr = get_binop_right(right);
4497
4498                                 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4499                                         ir_node *tmp = rl;
4500                                         rl = rr;
4501                                         rr = tmp;
4502                                 }
4503                                 if (rl == left) {
4504                                         ir_graph *irg = get_irn_irg(n);
4505                                         left     = rr;
4506                                         right   = create_zero_const(irg, mode);
4507                                         changed = true;
4508                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4509                                 }
4510                         }
4511
4512                         if (is_And(left) && is_Const(right)) {
4513                                 ir_node *ll = get_binop_left(left);
4514                                 ir_node *lr = get_binop_right(left);
4515                                 if (is_Shr(ll) && is_Const(lr)) {
4516                                         /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4517                                         ir_node *block = get_nodes_block(n);
4518                                         ir_mode *mode = get_irn_mode(left);
4519
4520                                         ir_node *llr = get_Shr_right(ll);
4521                                         if (is_Const(llr)) {
4522                                                 dbg_info *dbg = get_irn_dbg_info(left);
4523                                                 ir_graph *irg = get_irn_irg(left);
4524
4525                                                 ir_tarval *c1    = get_Const_tarval(llr);
4526                                                 ir_tarval *c2    = get_Const_tarval(lr);
4527                                                 ir_tarval *c3    = get_Const_tarval(right);
4528                                                 ir_tarval *mask  = tarval_shl(c2, c1);
4529                                                 ir_tarval *value = tarval_shl(c3, c1);
4530
4531                                                 left  = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4532                                                 right = new_r_Const(irg, value);
4533                                                 changed = true;
4534                                         }
4535                                 }
4536                         }
4537                         /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4538                          * cases */
4539                         if (is_Const(right) && is_Const_null(right) &&
4540                             (is_Eor(left) || is_Or_Eor_Add(left))) {
4541                                 right = get_Eor_right(left);
4542                                 left  = get_Eor_left(left);
4543                                 changed = true;
4544                         }
4545                 }
4546         }
4547
4548         if (mode_is_int(mode) && is_And(left)) {
4549                 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4550                  * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4551                 if (relation == ir_relation_equal
4552                 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4553                 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4554                         ir_node *and0 = get_And_left(left);
4555                         ir_node *and1 = get_And_right(left);
4556                         if (and1 == right) {
4557                                 ir_node *tmp = and0;
4558                                 and0 = and1;
4559                                 and1 = tmp;
4560                         }
4561                         if (and0 == right && is_single_bit(and0)) {
4562                                 ir_graph *irg = get_irn_irg(n);
4563                                 relation =
4564                                         relation == ir_relation_equal ? ir_relation_less_greater
4565                                                                       : ir_relation_equal;
4566                                 right = create_zero_const(irg, mode);
4567                                 changed |= 1;
4568                                 goto is_bittest;
4569                         }
4570                 }
4571
4572                 if (is_Const(right) && is_Const_null(right) &&
4573                     (relation == ir_relation_equal
4574                     || (relation == ir_relation_less_greater)
4575                     || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4576 is_bittest: {
4577                         /* instead of flipping the bit before the bit-test operation negate
4578                          * pnc */
4579                         ir_node *and0 = get_And_left(left);
4580                         ir_node *and1 = get_And_right(left);
4581                         if (is_Const(and1)) {
4582                                 ir_tarval *tv = get_Const_tarval(and1);
4583                                 if (tarval_is_single_bit(tv)) {
4584                                         ir_node *flipped = flips_bit(and0, tv);
4585                                         if (flipped != NULL) {
4586                                                 dbg_info *dbgi  = get_irn_dbg_info(left);
4587                                                 ir_node  *block = get_nodes_block(left);
4588                                                 relation = get_negated_relation(relation);
4589                                                 left = new_rd_And(dbgi, block, flipped, and1, mode);
4590                                                 changed |= 1;
4591                                         }
4592                                 }
4593                         }
4594                         }
4595                 }
4596         }
4597
4598         /* replace mode_b compares with ands/ors */
4599         if (mode == mode_b) {
4600                 ir_node  *block = get_nodes_block(n);
4601                 ir_node  *bres;
4602
4603                 switch (relation) {
4604                         case ir_relation_less_equal:
4605                                 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4606                                 break;
4607                         case ir_relation_less:
4608                                 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4609                                 break;
4610                         case ir_relation_greater_equal:
4611                                 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4612                                 break;
4613                         case ir_relation_greater:
4614                                 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4615                                 break;
4616                         case ir_relation_less_greater:
4617                                 bres = new_r_Eor(block, left, right, mode_b);
4618                                 break;
4619                         case ir_relation_equal:
4620                                 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4621                                 break;
4622                         default:
4623 #ifdef DEBUG_libfirm
4624                                 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4625 #endif
4626                                 bres = NULL;
4627                 }
4628                 if (bres != NULL) {
4629                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4630                         return bres;
4631                 }
4632         }
4633
4634         /*
4635          * First step: normalize the compare op
4636          * by placing the constant on the right side
4637          * or moving the lower address node to the left.
4638          */
4639         if (!operands_are_normalized(left, right)) {
4640                 ir_node *t = left;
4641                 left  = right;
4642                 right = t;
4643
4644                 relation = get_inversed_relation(relation);
4645                 changed  = true;
4646         }
4647
4648         /*
4649          * Second step: Try to reduce the magnitude
4650          * of a constant. This may help to generate better code
4651          * later and may help to normalize more compares.
4652          * Of course this is only possible for integer values.
4653          */
4654         tv = value_of(right);
4655         if (tv != tarval_bad) {
4656                 ir_mode *mode = get_irn_mode(right);
4657
4658                 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4659                  *   cmp(ct,c2) | cmp(cf,c2) | result
4660                  *   -----------|------------|--------
4661                  *   true       | true       | True
4662                  *   false      | false      | False
4663                  *   true       | false      | x
4664                  *   false      | true       | not(x)
4665                  */
4666                 if (is_Mux(left)) {
4667                         ir_node *mux_true  = get_Mux_true(left);
4668                         ir_node *mux_false = get_Mux_false(left);
4669                         if (is_Const(mux_true) && is_Const(mux_false)) {
4670                                 /* we can fold true/false constant separately */
4671                                 ir_tarval *tv_true  = get_Const_tarval(mux_true);
4672                                 ir_tarval *tv_false = get_Const_tarval(mux_false);
4673                                 ir_relation r_true  = tarval_cmp(tv_true, tv);
4674                                 ir_relation r_false = tarval_cmp(tv_false, tv);
4675                                 if (r_true != ir_relation_false
4676                                     || r_false != ir_relation_false) {
4677                                         bool rel_true  = (r_true & relation)  != 0;
4678                                         bool rel_false = (r_false & relation) != 0;
4679                                         ir_node *cond = get_Mux_sel(left);
4680                                         if (rel_true == rel_false) {
4681                                                 relation = rel_true ? ir_relation_true
4682                                                                     : ir_relation_false;
4683                                         } else if (rel_true) {
4684                                                 return cond;
4685                                         } else {
4686                                                 dbg_info *dbgi  = get_irn_dbg_info(n);
4687                                                 ir_node  *block = get_nodes_block(n);
4688                                                 ir_node  *notn  = new_rd_Not(dbgi, block, cond, mode_b);
4689                                                 return notn;
4690                                         }
4691                                 }
4692                         }
4693                 }
4694
4695                 /* TODO extend to arbitrary constants */
4696                 if (is_Conv(left) && tarval_is_null(tv)) {
4697                         ir_node *op      = get_Conv_op(left);
4698                         ir_mode *op_mode = get_irn_mode(op);
4699
4700                         /*
4701                          * UpConv(x) REL 0  ==> x REL 0
4702                          * Don't do this for float values as it's unclear whether it is a
4703                          * win. (on the other side it makes detection/creation of fabs hard)
4704                          */
4705                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4706                             ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4707                                  mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4708                                 !mode_is_float(mode)) {
4709                                 tv   = get_mode_null(op_mode);
4710                                 left = op;
4711                                 mode = op_mode;
4712                                 changedc = true;
4713                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4714                         }
4715                 }
4716
4717                 if (tv != tarval_bad) {
4718                         /* the following optimization is possible on modes without Overflow
4719                          * on Unary Minus or on == and !=:
4720                          * -a CMP c  ==>  a swap(CMP) -c
4721                          *
4722                          * Beware: for two-complement Overflow may occur, so only == and != can
4723                          * be optimized, see this:
4724                          * -MININT < 0 =/=> MININT > 0 !!!
4725                          */
4726                         if (is_Minus(left) &&
4727                                 (!mode_overflow_on_unary_Minus(mode) ||
4728                                 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4729                                 tv = tarval_neg(tv);
4730
4731                                 if (tv != tarval_bad) {
4732                                         left = get_Minus_op(left);
4733                                         relation = get_inversed_relation(relation);
4734                                         changedc = true;
4735                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4736                                 }
4737                         } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4738                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
4739                                 tv = tarval_not(tv);
4740
4741                                 if (tv != tarval_bad) {
4742                                         left = get_Not_op(left);
4743                                         changedc = true;
4744                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4745                                 }
4746                         }
4747
4748                         /* for integer modes, we have more */
4749                         if (mode_is_int(mode) && !is_Const(left)) {
4750                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
4751                                 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4752                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4753                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4754
4755                                         if (tv != tarval_bad) {
4756                                                 relation ^= ir_relation_equal;
4757                                                 changedc = true;
4758                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4759                                         }
4760                                 }
4761                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4762                                 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4763                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4764                                         tv = tarval_add(tv, get_mode_one(mode));
4765
4766                                         if (tv != tarval_bad) {
4767                                                 relation ^= ir_relation_equal;
4768                                                 changedc = true;
4769                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4770                                         }
4771                                 }
4772
4773                                 /* the following reassociations work only for == and != */
4774                                 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4775                                         if (tv != tarval_bad) {
4776                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4777                                                 if (is_Sub(left)) {
4778                                                         ir_node *c1 = get_Sub_right(left);
4779                                                         ir_tarval *tv2 = value_of(c1);
4780
4781                                                         if (tv2 != tarval_bad) {
4782                                                                 tv2 = tarval_add(tv, value_of(c1));
4783
4784                                                                 if (tv2 != tarval_bad) {
4785                                                                         left    = get_Sub_left(left);
4786                                                                         tv      = tv2;
4787                                                                         changedc = true;
4788                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4789                                                                 }
4790                                                         }
4791                                                 }
4792                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4793                                                 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4794                                                         ir_node *a_l = get_binop_left(left);
4795                                                         ir_node *a_r = get_binop_right(left);
4796                                                         ir_node *a;
4797                                                         ir_tarval *tv2;
4798
4799                                                         if (is_Const(a_l)) {
4800                                                                 a = a_r;
4801                                                                 tv2 = value_of(a_l);
4802                                                         } else {
4803                                                                 a = a_l;
4804                                                                 tv2 = value_of(a_r);
4805                                                         }
4806
4807                                                         if (tv2 != tarval_bad) {
4808                                                                 tv2 = tarval_sub(tv, tv2, NULL);
4809
4810                                                                 if (tv2 != tarval_bad) {
4811                                                                         left    = a;
4812                                                                         tv      = tv2;
4813                                                                         changedc = true;
4814                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4815                                                                 }
4816                                                         }
4817                                                 }
4818                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4819                                                 else if (is_Minus(left)) {
4820                                                         ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4821
4822                                                         if (tv2 != tarval_bad) {
4823                                                                 left    = get_Minus_op(left);
4824                                                                 tv      = tv2;
4825                                                                 changedc = true;
4826                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4827                                                         }
4828                                                 }
4829                                         }
4830                                 }
4831                         }
4832
4833                         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4834                                 switch (get_irn_opcode(left)) {
4835                                         ir_node *c1;
4836
4837                                 case iro_And:
4838                                         c1 = get_And_right(left);
4839                                         if (is_Const(c1)) {
4840                                                 /*
4841                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4842                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4843                                                  */
4844                                                 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4845                                                 if (mask != tv) {
4846                                                         /* TODO: move to constant evaluation */
4847                                                         ir_graph *irg = get_irn_irg(n);
4848                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4849                                                         c1 = new_r_Const(irg, tv);
4850                                                         DBG_OPT_CSTEVAL(n, c1);
4851                                                         return c1;
4852                                                 }
4853
4854                                                 if (tarval_is_single_bit(tv)) {
4855                                                         /*
4856                                                          * optimization for AND:
4857                                                          * Optimize:
4858                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4859                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4860                                                          *
4861                                                          * if C is a single Bit constant.
4862                                                          */
4863
4864                                                         /* check for Constant's match. We have check hare the tarvals,
4865                                                            because our const might be changed */
4866                                                         if (get_Const_tarval(c1) == tv) {
4867                                                                 /* fine: do the transformation */
4868                                                                 tv = get_mode_null(get_tarval_mode(tv));
4869                                                                 relation ^= ir_relation_less_equal_greater;
4870                                                                 changedc = true;
4871                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4872                                                         }
4873                                                 }
4874                                         }
4875                                         break;
4876                                 case iro_Or:
4877                                         c1 = get_Or_right(left);
4878                                         if (is_Const(c1) && tarval_is_null(tv)) {
4879                                                 /*
4880                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4881                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4882                                                  */
4883                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4884                                                         /* TODO: move to constant evaluation */
4885                                                         ir_graph *irg = get_irn_irg(n);
4886                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4887                                                         c1 = new_r_Const(irg, tv);
4888                                                         DBG_OPT_CSTEVAL(n, c1);
4889                                                         return c1;
4890                                                 }
4891                                         }
4892                                         break;
4893                                 case iro_Shl:
4894                                         /*
4895                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4896                                          *                             FALSE                       else
4897                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4898                                          *                             TRUE                        else
4899                                          */
4900                                         c1 = get_Shl_right(left);
4901                                         if (is_Const(c1)) {
4902                                                 ir_graph  *irg    = get_irn_irg(c1);
4903                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4904                                                 ir_mode   *mode   = get_irn_mode(left);
4905                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4906                                                 ir_tarval *amask  = tarval_shr(minus1, tv1);
4907                                                 ir_tarval *cmask  = tarval_shl(minus1, tv1);
4908                                                 ir_node   *sl, *blk;
4909
4910                                                 if (tarval_and(tv, cmask) != tv) {
4911                                                         /* condition not met */
4912                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4913                                                         c1 = new_r_Const(irg, tv);
4914                                                         DBG_OPT_CSTEVAL(n, c1);
4915                                                         return c1;
4916                                                 }
4917                                                 sl   = get_Shl_left(left);
4918                                                 blk  = get_nodes_block(n);
4919                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4920                                                 tv   = tarval_shr(tv, tv1);
4921                                                 changedc = true;
4922                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4923                                         }
4924                                         break;
4925                                 case iro_Shr:
4926                                         /*
4927                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4928                                          *                             FALSE                       else
4929                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4930                                          *                             TRUE                        else
4931                                          */
4932                                         c1 = get_Shr_right(left);
4933                                         if (is_Const(c1)) {
4934                                                 ir_graph  *irg    = get_irn_irg(c1);
4935                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4936                                                 ir_mode   *mode   = get_irn_mode(left);
4937                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4938                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4939                                                 ir_tarval *cmask  = tarval_shr(minus1, tv1);
4940                                                 ir_node   *sl, *blk;
4941
4942                                                 if (tarval_and(tv, cmask) != tv) {
4943                                                         /* condition not met */
4944                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4945                                                         c1 = new_r_Const(irg, tv);
4946                                                         DBG_OPT_CSTEVAL(n, c1);
4947                                                         return c1;
4948                                                 }
4949                                                 sl   = get_Shr_left(left);
4950                                                 blk  = get_nodes_block(n);
4951                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4952                                                 tv   = tarval_shl(tv, tv1);
4953                                                 changedc = true;
4954                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4955                                         }
4956                                         break;
4957                                 case iro_Shrs:
4958                                         /*
4959                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4960                                          *                             FALSE                       else
4961                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4962                                          *                             TRUE                        else
4963                                          */
4964                                         c1 = get_Shrs_right(left);
4965                                         if (is_Const(c1)) {
4966                                                 ir_graph  *irg    = get_irn_irg(c1);
4967                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4968                                                 ir_mode   *mode   = get_irn_mode(left);
4969                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4970                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4971                                                 ir_tarval *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4972                                                 ir_node *sl, *blk;
4973
4974                                                 cond = tarval_sub(cond, tv1, NULL);
4975                                                 cond = tarval_shrs(tv, cond);
4976
4977                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4978                                                         /* condition not met */
4979                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4980                                                         c1 = new_r_Const(irg, tv);
4981                                                         DBG_OPT_CSTEVAL(n, c1);
4982                                                         return c1;
4983                                                 }
4984                                                 sl   = get_Shrs_left(left);
4985                                                 blk  = get_nodes_block(n);
4986                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4987                                                 tv   = tarval_shl(tv, tv1);
4988                                                 changedc = true;
4989                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4990                                         }
4991                                         break;
4992                                 }
4993                         }
4994                 }
4995         }
4996
4997         if (changedc) {     /* need a new Const */
4998                 ir_graph *irg = get_irn_irg(n);
4999                 right = new_r_Const(irg, tv);
5000                 changed = true;
5001         }
5002
5003         if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
5004                 ir_node *op = get_Proj_pred(left);
5005
5006                 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
5007                         ir_node *c = get_binop_right(op);
5008
5009                         if (is_Const(c)) {
5010                                 ir_tarval *tv = get_Const_tarval(c);
5011
5012                                 if (tarval_is_single_bit(tv)) {
5013                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5014                                         ir_node *v    = get_binop_left(op);
5015                                         ir_node *blk  = get_irn_n(op, -1);
5016                                         ir_graph *irg = get_irn_irg(op);
5017                                         ir_mode *mode = get_irn_mode(v);
5018
5019                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
5020                                         left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5021                                         changed = true;
5022                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5023                                 }
5024                         }
5025                 }
5026         }
5027
5028         if (changed) {
5029                 dbg_info *dbgi  = get_irn_dbg_info(n);
5030                 ir_node  *block = get_nodes_block(n);
5031
5032                 /* create a new compare */
5033                 n = new_rd_Cmp(dbgi, block, left, right, relation);
5034         }
5035
5036         return n;
5037 }
5038
5039 /**
5040  * Optimize CopyB(mem, x, x) into a Nop.
5041  */
5042 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5043 {
5044         ir_node *copyb = get_Proj_pred(proj);
5045         ir_node *a     = get_CopyB_dst(copyb);
5046         ir_node *b     = get_CopyB_src(copyb);
5047
5048         if (a == b) {
5049                 switch (get_Proj_proj(proj)) {
5050                 case pn_CopyB_X_regular:
5051                         /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5052                         DBG_OPT_EXC_REM(proj);
5053                         proj = new_r_Jmp(get_nodes_block(copyb));
5054                         break;
5055                 case pn_CopyB_X_except: {
5056                         ir_graph *irg = get_irn_irg(proj);
5057                         DBG_OPT_EXC_REM(proj);
5058                         proj = new_r_Bad(irg, mode_X);
5059                         break;
5060                 }
5061                 default:
5062                         break;
5063                 }
5064         }
5065         return proj;
5066 }
5067
5068 /**
5069  * Optimize Bounds(idx, idx, upper) into idx.
5070  */
5071 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5072 {
5073         ir_node *oldn  = proj;
5074         ir_node *bound = get_Proj_pred(proj);
5075         ir_node *idx   = get_Bound_index(bound);
5076         ir_node *pred  = skip_Proj(idx);
5077         int ret_tuple  = 0;
5078
5079         if (idx == get_Bound_lower(bound))
5080                 ret_tuple = 1;
5081         else if (is_Bound(pred)) {
5082                 /*
5083                 * idx was Bounds checked previously, it is still valid if
5084                 * lower <= pred_lower && pred_upper <= upper.
5085                 */
5086                 ir_node *lower = get_Bound_lower(bound);
5087                 ir_node *upper = get_Bound_upper(bound);
5088                 if (get_Bound_lower(pred) == lower &&
5089                         get_Bound_upper(pred) == upper) {
5090                         /*
5091                          * One could expect that we simply return the previous
5092                          * Bound here. However, this would be wrong, as we could
5093                          * add an exception Proj to a new location then.
5094                          * So, we must turn in into a tuple.
5095                          */
5096                         ret_tuple = 1;
5097                 }
5098         }
5099         if (ret_tuple) {
5100                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5101                 switch (get_Proj_proj(proj)) {
5102                 case pn_Bound_M:
5103                         DBG_OPT_EXC_REM(proj);
5104                         proj = get_Bound_mem(bound);
5105                         break;
5106                 case pn_Bound_X_except:
5107                         DBG_OPT_EXC_REM(proj);
5108                         proj = new_r_Bad(get_irn_irg(proj), mode_X);
5109                         break;
5110                 case pn_Bound_res:
5111                         proj = idx;
5112                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5113                         break;
5114                 case pn_Bound_X_regular:
5115                         DBG_OPT_EXC_REM(proj);
5116                         proj = new_r_Jmp(get_nodes_block(bound));
5117                         break;
5118                 default:
5119                         break;
5120                 }
5121         }
5122         return proj;
5123 }
5124
5125 /**
5126  * Does all optimizations on nodes that must be done on its Projs
5127  * because of creating new nodes.
5128  */
5129 static ir_node *transform_node_Proj(ir_node *proj)
5130 {
5131         ir_node *n = get_Proj_pred(proj);
5132
5133         if (n->op->ops.transform_node_Proj)
5134                 return n->op->ops.transform_node_Proj(proj);
5135         return proj;
5136 }
5137
5138 /**
5139  * Test whether a block is unreachable
5140  * Note: That this only returns true when
5141  * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5142  * This is important, as you easily end up producing invalid constructs in the
5143  * unreachable code when optimizing away edges into the unreachable code.
5144  * So only set this flag when you iterate localopts to the fixpoint.
5145  * When you reach the fixpoint then all unreachable code is dead
5146  * (= can't be reached by firm edges) and you won't see the invalid constructs
5147  * anymore.
5148  */
5149 static bool is_block_unreachable(const ir_node *block)
5150 {
5151         const ir_graph *irg = get_irn_irg(block);
5152         if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5153                 return false;
5154         return get_Block_dom_depth(block) < 0;
5155 }
5156
5157 static ir_node *transform_node_Block(ir_node *block)
5158 {
5159         ir_graph *irg   = get_irn_irg(block);
5160         int       arity = get_irn_arity(block);
5161         ir_node  *bad   = NULL;
5162         int       i;
5163
5164         if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5165                 return block;
5166
5167         for (i = 0; i < arity; ++i) {
5168                 ir_node *const pred = get_Block_cfgpred(block, i);
5169                 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5170                         continue;
5171                 if (bad == NULL)
5172                         bad = new_r_Bad(irg, mode_X);
5173                 set_irn_n(block, i, bad);
5174         }
5175
5176         return block;
5177 }
5178
5179 static ir_node *transform_node_Phi(ir_node *phi)
5180 {
5181         int       n     = get_irn_arity(phi);
5182         ir_mode  *mode  = get_irn_mode(phi);
5183         ir_node  *block = get_nodes_block(phi);
5184         ir_graph *irg   = get_irn_irg(phi);
5185         ir_node  *bad   = NULL;
5186         int       i;
5187
5188         /* Set phi-operands for bad-block inputs to bad */
5189         for (i = 0; i < n; ++i) {
5190                 if (!is_Bad(get_Phi_pred(phi, i))) {
5191                         ir_node *pred = get_Block_cfgpred(block, i);
5192                         if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5193                                 if (bad == NULL)
5194                                         bad = new_r_Bad(irg, mode);
5195                                 set_irn_n(phi, i, bad);
5196                         }
5197                 }
5198         }
5199
5200         /* Move Pin nodes down through Phi nodes. */
5201         if (mode == mode_M) {
5202                 n = get_irn_arity(phi);
5203
5204                 /* Beware of Phi0 */
5205                 if (n > 0) {
5206                         ir_node **in;
5207                         ir_node  *new_phi;
5208                         bool      has_pin = false;
5209
5210                         NEW_ARR_A(ir_node *, in, n);
5211
5212                         for (i = 0; i < n; ++i) {
5213                                 ir_node *pred = get_irn_n(phi, i);
5214
5215                                 if (is_Pin(pred)) {
5216                                         in[i]   = get_Pin_op(pred);
5217                                         has_pin = true;
5218                                 } else if (is_Bad(pred)) {
5219                                         in[i] = pred;
5220                                 } else {
5221                                         return phi;
5222                                 }
5223                         }
5224
5225                         if (!has_pin)
5226                                 return phi;
5227
5228                         /* Move the Pin nodes "behind" the Phi. */
5229                         block   = get_irn_n(phi, -1);
5230                         new_phi = new_r_Phi(block, n, in, mode_M);
5231                         return new_r_Pin(block, new_phi);
5232                 }
5233         }
5234         /* Move Confirms down through Phi nodes. */
5235         else if (mode_is_reference(mode)) {
5236                 n = get_irn_arity(phi);
5237
5238                 /* Beware of Phi0 */
5239                 if (n > 0) {
5240                         ir_node    *pred = get_irn_n(phi, 0);
5241                         ir_node    *bound, *new_phi, *block, **in;
5242                         ir_relation relation;
5243                         bool        has_confirm = false;
5244
5245                         if (! is_Confirm(pred))
5246                                 return phi;
5247
5248                         bound    = get_Confirm_bound(pred);
5249                         relation = get_Confirm_relation(pred);
5250
5251                         NEW_ARR_A(ir_node *, in, n);
5252                         in[0] = get_Confirm_value(pred);
5253
5254                         for (i = 1; i < n; ++i) {
5255                                 pred = get_irn_n(phi, i);
5256
5257                                 if (is_Confirm(pred) &&
5258                                                 get_Confirm_bound(pred) == bound &&
5259                                                 get_Confirm_relation(pred) == relation) {
5260                                         in[i]       = get_Confirm_value(pred);
5261                                         has_confirm = true;
5262                                 } else if (is_Bad(pred)) {
5263                                         in[i] = pred;
5264                                 } else {
5265                                         return phi;
5266                                 }
5267                         }
5268
5269                         if (!has_confirm)
5270                                 return phi;
5271
5272                         /* move the Confirm nodes "behind" the Phi */
5273                         block = get_irn_n(phi, -1);
5274                         new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5275                         return new_r_Confirm(block, new_phi, bound, relation);
5276                 }
5277         }
5278         return phi;
5279 }
5280
5281 /**
5282  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5283  *
5284  * Should be moved to reassociation?
5285  */
5286 static ir_node *transform_node_shift(ir_node *n)
5287 {
5288         ir_node *left, *right;
5289         ir_mode *mode;
5290         ir_mode *count_mode;
5291         ir_tarval *tv1, *tv2, *res;
5292         ir_node *in[2], *irn, *block;
5293         ir_graph *irg;
5294         int       modulo_shf;
5295
5296         left = get_binop_left(n);
5297
5298         /* different operations */
5299         if (get_irn_op(left) != get_irn_op(n))
5300                 return n;
5301
5302         right = get_binop_right(n);
5303         tv1   = value_of(right);
5304         if (tv1 == tarval_bad)
5305                 return n;
5306
5307         tv2 = value_of(get_binop_right(left));
5308         if (tv2 == tarval_bad)
5309                 return n;
5310
5311         count_mode = get_tarval_mode(tv1);
5312         if (get_tarval_mode(tv2) != count_mode) {
5313                 /* TODO: search bigger mode or something and convert... */
5314                 return n;
5315         }
5316
5317         mode       = get_irn_mode(n);
5318         modulo_shf = get_mode_modulo_shift(mode);
5319
5320         if (modulo_shf > 0) {
5321                 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5322
5323                 /* I'm not so sure what happens in one complement... */
5324                 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5325                 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5326                  * above will be invalid) */
5327                 assert(modulo_shf<=0 || is_po2(modulo_shf));
5328
5329                 tv1 = tarval_and(tv1, modulo_mask);
5330                 tv2 = tarval_and(tv2, modulo_mask);
5331         }
5332         res = tarval_add(tv1, tv2);
5333         irg = get_irn_irg(n);
5334
5335         /* beware: a simple replacement works only, if res < modulo shift */
5336         if (is_Rotl(n)) {
5337                 int        bits   = get_mode_size_bits(mode);
5338                 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5339                 res = tarval_mod(res, modulo);
5340         } else {
5341                 long       bits      = get_mode_size_bits(mode);
5342                 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5343
5344                 /* shifting too much */
5345                 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5346                         if (is_Shrs(n)) {
5347                                 ir_node  *block = get_nodes_block(n);
5348                                 dbg_info *dbgi  = get_irn_dbg_info(n);
5349                                 ir_mode  *smode = get_irn_mode(right);
5350                                 ir_node  *cnst  = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5351                                 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5352                         }
5353
5354                         return new_r_Const(irg, get_mode_null(mode));
5355                 }
5356         }
5357
5358         /* ok, we can replace it */
5359         assert(modulo_shf >= (int) get_mode_size_bits(mode));
5360         block = get_nodes_block(n);
5361
5362         in[0] = get_binop_left(left);
5363         in[1] = new_r_Const(irg, res);
5364
5365         irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5366
5367         DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5368
5369         return irn;
5370 }
5371
5372 /**
5373  * normalisation:
5374  *    (x << c1) >> c2  <=>  x OP (c2-c1) & ((-1 << c1) >> c2)
5375  *    also:
5376  *    (x >> c1) << c2  <=>  x OP (c2-c1) & ((-1 >> c1) << c2)
5377  *      (also with x >>s c1  when c1>=c2)
5378  */
5379 static ir_node *transform_node_shl_shr(ir_node *n)
5380 {
5381         ir_node   *left;
5382         ir_node   *right = get_binop_right(n);
5383         ir_node   *x;
5384         ir_node   *block;
5385         ir_mode   *mode;
5386         dbg_info  *dbgi;
5387         ir_node   *new_const;
5388         ir_node   *new_shift;
5389         ir_node   *new_and;
5390         ir_tarval *tv_shl;
5391         ir_tarval *tv_shr;
5392         ir_tarval *tv_shift;
5393         ir_tarval *tv_mask;
5394         ir_graph  *irg;
5395         ir_relation relation;
5396         int        need_shrs = 0;
5397
5398         assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5399
5400         if (!is_Const(right))
5401                 return n;
5402
5403         left = get_binop_left(n);
5404         mode = get_irn_mode(n);
5405         if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5406                 ir_node *shr_right = get_binop_right(left);
5407
5408                 if (!is_Const(shr_right))
5409                         return n;
5410
5411                 x      = get_binop_left(left);
5412                 tv_shr = get_Const_tarval(shr_right);
5413                 tv_shl = get_Const_tarval(right);
5414
5415                 if (is_Shrs(left)) {
5416                         /* shrs variant only allowed if c1 >= c2 */
5417                         if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5418                                 return n;
5419
5420                         tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5421                         need_shrs = 1;
5422                 } else {
5423                         tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5424                 }
5425                 tv_mask = tarval_shl(tv_mask, tv_shl);
5426         } else if (is_Shr(n) && is_Shl(left)) {
5427                 ir_node *shl_right = get_Shl_right(left);
5428
5429                 if (!is_Const(shl_right))
5430                         return n;
5431
5432                 x      = get_Shl_left(left);
5433                 tv_shr = get_Const_tarval(right);
5434                 tv_shl = get_Const_tarval(shl_right);
5435
5436                 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5437                 tv_mask = tarval_shr(tv_mask, tv_shr);
5438         } else {
5439                 return n;
5440         }
5441
5442         if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5443                 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5444         }
5445
5446         assert(tv_mask != tarval_bad);
5447         assert(get_tarval_mode(tv_mask) == mode);
5448
5449         block = get_nodes_block(n);
5450         irg   = get_irn_irg(block);
5451         dbgi  = get_irn_dbg_info(n);
5452
5453         relation = tarval_cmp(tv_shl, tv_shr);
5454         if (relation == ir_relation_less || relation == ir_relation_equal) {
5455                 tv_shift  = tarval_sub(tv_shr, tv_shl, NULL);
5456                 new_const = new_r_Const(irg, tv_shift);
5457                 if (need_shrs) {
5458                         new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5459                 } else {
5460                         new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5461                 }
5462         } else {
5463                 assert(relation == ir_relation_greater);
5464                 tv_shift  = tarval_sub(tv_shl, tv_shr, NULL);
5465                 new_const = new_r_Const(irg, tv_shift);
5466                 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5467         }
5468
5469         new_const = new_r_Const(irg, tv_mask);
5470         new_and   = new_rd_And(dbgi, block, new_shift, new_const, mode);
5471
5472         return new_and;
5473 }
5474
5475 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5476 {
5477         ir_mode   *mode      = get_tarval_mode(tv);
5478         ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5479         return tarval_mod(tv, modulo_tv);
5480 }
5481
5482 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5483                                   ir_node *left, ir_node *right, ir_mode *mode);
5484
5485 /**
5486  * Normalisation: if we have a shl/shr with modulo_shift behaviour
5487  * then we can use that to minimize the value of Add(x, const) or
5488  * Sub(Const, x). In particular this often avoids 1 instruction in some
5489  * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5490  * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5491  */
5492 static ir_node *transform_node_shift_modulo(ir_node *n,
5493                                             new_shift_func new_shift)
5494 {
5495         ir_mode  *mode   = get_irn_mode(n);
5496         int       modulo = get_mode_modulo_shift(mode);
5497         ir_node  *newop  = NULL;
5498         ir_mode  *mode_right;
5499         ir_node  *block;
5500         ir_node  *right;
5501         ir_graph *irg;
5502
5503         if (modulo == 0)
5504                 return n;
5505         if (get_mode_arithmetic(mode) != irma_twos_complement)
5506                 return n;
5507         if (!is_po2(modulo))
5508                 return n;
5509
5510         irg        = get_irn_irg(n);
5511         block      = get_nodes_block(n);
5512         right      = get_binop_right(n);
5513         mode_right = get_irn_mode(right);
5514         if (is_Const(right)) {
5515                 ir_tarval *tv     = get_Const_tarval(right);
5516                 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5517
5518                 if (tv_mod == tv)
5519                         return n;
5520
5521                 newop = new_r_Const(irg, tv_mod);
5522         } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5523                 ir_node *add_right = get_binop_right(right);
5524                 if (is_Const(add_right)) {
5525                         ir_tarval *tv     = get_Const_tarval(add_right);
5526                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5527                         ir_node   *newconst;
5528                         if (tv_mod == tv)
5529                                 return n;
5530
5531                         newconst = new_r_Const(irg, tv_mod);
5532                         newop    = new_r_Add(block, get_binop_left(right), newconst,
5533                                              mode_right);
5534                 }
5535         } else if (is_Sub(right)) {
5536                 ir_node *sub_left = get_Sub_left(right);
5537                 if (is_Const(sub_left)) {
5538                         ir_tarval *tv     = get_Const_tarval(sub_left);
5539                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5540                         ir_node  *newconst;
5541                         if (tv_mod == tv)
5542                                 return n;
5543
5544                         newconst = new_r_Const(irg, tv_mod);
5545                         newop    = new_r_Sub(block, newconst, get_Sub_right(right),
5546                                              mode_right);
5547                 }
5548         } else {
5549                 return n;
5550         }
5551
5552         if (newop != NULL) {
5553                 dbg_info *dbgi = get_irn_dbg_info(n);
5554                 ir_node  *left = get_binop_left(n);
5555                 return new_shift(dbgi, block, left, newop, mode);
5556         }
5557         return n;
5558 }
5559
5560 /**
5561  * Transform a Shr.
5562  */
5563 static ir_node *transform_node_Shr(ir_node *n)
5564 {
5565         ir_node *c, *oldn = n;
5566         ir_node *left  = get_Shr_left(n);
5567         ir_node *right = get_Shr_right(n);
5568         ir_mode *mode  = get_irn_mode(n);
5569
5570         HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5571         n = transform_node_shift(n);
5572
5573         if (is_Shr(n))
5574                 n = transform_node_shift_modulo(n, new_rd_Shr);
5575         if (is_Shr(n))
5576                 n = transform_node_shl_shr(n);
5577         if (is_Shr(n))
5578                 n = transform_node_shift_bitop(n);
5579
5580         return n;
5581 }
5582
5583 /**
5584  * Transform a Shrs.
5585  */
5586 static ir_node *transform_node_Shrs(ir_node *n)
5587 {
5588         ir_node  *oldn = n;
5589         ir_node  *a    = get_Shrs_left(n);
5590         ir_node  *b    = get_Shrs_right(n);
5591         ir_mode  *mode = get_irn_mode(n);
5592         ir_node  *c;
5593         vrp_attr *attr;
5594
5595         if (is_oversize_shift(n)) {
5596                 ir_node  *block = get_nodes_block(n);
5597                 dbg_info *dbgi  = get_irn_dbg_info(n);
5598                 ir_mode  *cmode = get_irn_mode(b);
5599                 long      val   = get_mode_size_bits(cmode)-1;
5600                 ir_graph *irg   = get_irn_irg(n);
5601                 ir_node  *cnst  = new_r_Const_long(irg, cmode, val);
5602                 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5603         }
5604
5605         HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5606         n = transform_node_shift(n);
5607         if (n != oldn)
5608                 return n;
5609
5610         n = transform_node_shift_modulo(n, new_rd_Shrs);
5611         if (n != oldn)
5612                 return n;
5613         n = transform_node_shift_bitop(n);
5614         if (n != oldn)
5615                 return n;
5616
5617         /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5618         attr = vrp_get_info(a);
5619         if (attr != NULL) {
5620                 unsigned   bits   = get_mode_size_bits(mode);
5621                 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5622                 ir_tarval *sign   = tarval_shl(get_mode_one(mode), scount);
5623                 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5624                         dbg_info *dbgi  = get_irn_dbg_info(n);
5625                         ir_node  *block = get_nodes_block(n);
5626                         return new_rd_Shr(dbgi, block, a, b, mode);
5627                 }
5628         }
5629
5630         return n;
5631 }
5632
5633 /**
5634  * Transform a Shl.
5635  */
5636 static ir_node *transform_node_Shl(ir_node *n)
5637 {
5638         ir_node *c, *oldn = n;
5639         ir_node *a    = get_Shl_left(n);
5640         ir_node *b    = get_Shl_right(n);
5641         ir_mode *mode = get_irn_mode(n);
5642
5643         HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5644         n = transform_node_shift(n);
5645
5646         if (is_Shl(n))
5647                 n = transform_node_shift_modulo(n, new_rd_Shl);
5648         if (is_Shl(n))
5649                 n = transform_node_shl_shr(n);
5650         if (is_Shl(n))
5651                 n = transform_node_shift_bitop(n);
5652
5653         return n;
5654 }
5655
5656 /**
5657  * Transform a Rotl.
5658  */
5659 static ir_node *transform_node_Rotl(ir_node *n)
5660 {
5661         ir_node *c, *oldn = n;
5662         ir_node *a    = get_Rotl_left(n);
5663         ir_node *b    = get_Rotl_right(n);
5664         ir_mode *mode = get_irn_mode(n);
5665
5666         HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5667         n = transform_node_shift(n);
5668
5669         if (is_Rotl(n))
5670                 n = transform_node_shift_bitop(n);
5671
5672         return n;
5673 }
5674
5675 /**
5676  * Transform a Conv.
5677  */
5678 static ir_node *transform_node_Conv(ir_node *n)
5679 {
5680         ir_node *c, *oldn = n;
5681         ir_mode *mode = get_irn_mode(n);
5682         ir_node *a    = get_Conv_op(n);
5683
5684         if (mode != mode_b && is_const_Phi(a)) {
5685                 /* Do NOT optimize mode_b Conv's, this leads to remaining
5686                  * Phib nodes later, because the conv_b_lower operation
5687                  * is instantly reverted, when it tries to insert a Convb.
5688                  */
5689                 c = apply_conv_on_phi(a, mode);
5690                 if (c) {
5691                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5692                         return c;
5693                 }
5694         }
5695
5696         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5697                 ir_graph *irg = get_irn_irg(n);
5698                 return new_r_Unknown(irg, mode);
5699         }
5700
5701         if (mode_is_reference(mode) &&
5702                 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5703                 is_Add(a)) {
5704                 ir_node *l = get_Add_left(a);
5705                 ir_node *r = get_Add_right(a);
5706                 dbg_info *dbgi = get_irn_dbg_info(a);
5707                 ir_node *block = get_nodes_block(n);
5708                 if (is_Conv(l)) {
5709                         ir_node *lop = get_Conv_op(l);
5710                         if (get_irn_mode(lop) == mode) {
5711                                 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5712                                 n = new_rd_Add(dbgi, block, lop, r, mode);
5713                                 return n;
5714                         }
5715                 }
5716                 if (is_Conv(r)) {
5717                         ir_node *rop = get_Conv_op(r);
5718                         if (get_irn_mode(rop) == mode) {
5719                                 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5720                                 n = new_rd_Add(dbgi, block, l, rop, mode);
5721                                 return n;
5722                         }
5723                 }
5724         }
5725
5726         return n;
5727 }
5728
5729 /**
5730  * Remove dead blocks and nodes in dead blocks
5731  * in keep alive list.  We do not generate a new End node.
5732  */
5733 static ir_node *transform_node_End(ir_node *n)
5734 {
5735         int i, j, n_keepalives = get_End_n_keepalives(n);
5736         ir_node **in;
5737
5738         NEW_ARR_A(ir_node *, in, n_keepalives);
5739
5740         for (i = j = 0; i < n_keepalives; ++i) {
5741                 ir_node *ka = get_End_keepalive(n, i);
5742                 ir_node *block;
5743                 /* no need to keep Bad */
5744                 if (is_Bad(ka))
5745                         continue;
5746                 /* do not keep unreachable code */
5747                 block = is_Block(ka) ? ka : get_nodes_block(ka);
5748                 if (is_block_unreachable(block))
5749                         continue;
5750                 in[j++] = ka;
5751         }
5752         if (j != n_keepalives)
5753                 set_End_keepalives(n, j, in);
5754         return n;
5755 }
5756
5757 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5758 {
5759         if (is_Minus(a) && get_Minus_op(a) == b)
5760                 return true;
5761         if (is_Minus(b) && get_Minus_op(b) == a)
5762                 return true;
5763         if (is_Sub(a) && is_Sub(b)) {
5764                 ir_node *a_left  = get_Sub_left(a);
5765                 ir_node *a_right = get_Sub_right(a);
5766                 ir_node *b_left  = get_Sub_left(b);
5767                 ir_node *b_right = get_Sub_right(b);
5768
5769                 if (a_left == b_right && a_right == b_left)
5770                         return true;
5771         }
5772
5773         return false;
5774 }
5775
5776 static const ir_node *skip_upconv(const ir_node *node)
5777 {
5778         while (is_Conv(node)) {
5779                 ir_mode       *mode    = get_irn_mode(node);
5780                 const ir_node *op      = get_Conv_op(node);
5781                 ir_mode       *op_mode = get_irn_mode(op);
5782                 if (!smaller_mode(op_mode, mode))
5783                         break;
5784                 node = op;
5785         }
5786         return node;
5787 }
5788
5789 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5790                   const ir_node *mux_true)
5791 {
5792         ir_node    *cmp_left;
5793         ir_node    *cmp_right;
5794         ir_mode    *mode;
5795         ir_relation relation;
5796
5797         if (!is_Cmp(sel))
5798                 return 0;
5799
5800         /**
5801          * Note further that these optimization work even for floating point
5802          * with NaN's because -NaN == NaN.
5803          * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5804          * transformations.
5805          */
5806         mode = get_irn_mode(mux_true);
5807         if (mode_honor_signed_zeros(mode))
5808                 return 0;
5809
5810         /* must be <, <=, >=, > */
5811         relation = get_Cmp_relation(sel);
5812         if ((relation & ir_relation_less_greater) == 0)
5813                 return 0;
5814
5815         if (!ir_is_negated_value(mux_true, mux_false))
5816                 return 0;
5817
5818         mux_true  = skip_upconv(mux_true);
5819         mux_false = skip_upconv(mux_false);
5820
5821         /* must be x cmp 0 */
5822         cmp_right = get_Cmp_right(sel);
5823         if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5824                 return 0;
5825
5826         cmp_left = get_Cmp_left(sel);
5827         if (cmp_left == mux_false) {
5828                 if (relation & ir_relation_less) {
5829                         return 1;
5830                 } else {
5831                         assert(relation & ir_relation_greater);
5832                         return -1;
5833                 }
5834         } else if (cmp_left == mux_true) {
5835                 if (relation & ir_relation_less) {
5836                         return -1;
5837                 } else {
5838                         assert(relation & ir_relation_greater);
5839                         return 1;
5840                 }
5841         }
5842
5843         return 0;
5844 }
5845
5846 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5847                        ir_node *mux_true)
5848 {
5849         ir_node *cmp_left = get_Cmp_left(sel);
5850         return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5851 }
5852
5853 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5854                            const ir_node *mux_true)
5855 {
5856         /* this code should return true each time transform_node_Mux would
5857          * optimize the Mux completely away */
5858
5859         ir_mode *mode = get_irn_mode(mux_false);
5860         if (get_mode_arithmetic(mode) == irma_twos_complement
5861             && ir_mux_is_abs(sel, mux_false, mux_true))
5862             return true;
5863
5864         if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5865                 const ir_node *cmp_r = get_Cmp_right(sel);
5866                 const ir_node *cmp_l = get_Cmp_left(sel);
5867                 const ir_node *f     = mux_false;
5868                 const ir_node *t     = mux_true;
5869
5870                 if (is_Const(t) && is_Const_null(t)) {
5871                         t = mux_false;
5872                         f = mux_true;
5873                 }
5874
5875                 if (is_And(cmp_l) && f == cmp_r) {
5876                         ir_node *and_r = get_And_right(cmp_l);
5877                         ir_node *and_l;
5878
5879                         if (and_r == t && is_single_bit(and_r))
5880                                 return true;
5881                         and_l = get_And_left(cmp_l);
5882                         if (and_l == t && is_single_bit(and_l))
5883                                 return true;
5884                 }
5885         }
5886
5887         return false;
5888 }
5889
5890 /**
5891  * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5892  */
5893 static ir_node *transform_Mux_set(ir_node *n)
5894 {
5895         ir_node    *cond = get_Mux_sel(n);
5896         ir_mode    *dest_mode;
5897         ir_mode    *mode;
5898         ir_node    *left;
5899         ir_node    *right;
5900         ir_relation relation;
5901         bool        need_not;
5902         dbg_info   *dbgi;
5903         ir_node    *block;
5904         ir_graph   *irg;
5905         ir_node    *a;
5906         ir_node    *b;
5907         unsigned    bits;
5908         ir_tarval  *tv;
5909         ir_node    *shift_cnt;
5910         ir_node    *res;
5911
5912         if (!is_Cmp(cond))
5913                 return n;
5914         left = get_Cmp_left(cond);
5915         mode = get_irn_mode(left);
5916         if (!mode_is_int(mode) && !mode_is_reference(mode))
5917                 return n;
5918         dest_mode = get_irn_mode(n);
5919         right     = get_Cmp_right(cond);
5920         relation  = get_Cmp_relation(cond) & ~ir_relation_unordered;
5921         if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5922             && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5923                  && relation != ir_relation_greater))
5924             return n;
5925
5926         need_not = false;
5927         switch (relation) {
5928         case ir_relation_less:
5929                 /* a < b  ->  (a - b) >> 31 */
5930                 a = left;
5931                 b = right;
5932                 break;
5933         case ir_relation_less_equal:
5934                 /* a <= b  -> ~(a - b) >> 31 */
5935                 a        = right;
5936                 b        = left;
5937                 need_not = true;
5938                 break;
5939         case ir_relation_greater:
5940                 /* a > b   -> (b - a) >> 31 */
5941                 a = right;
5942                 b = left;
5943                 break;
5944         case ir_relation_greater_equal:
5945                 /* a >= b   -> ~(a - b) >> 31 */
5946                 a        = left;
5947                 b        = right;
5948                 need_not = true;
5949                 break;
5950         default:
5951                 return n;
5952         }
5953
5954         dbgi      = get_irn_dbg_info(n);
5955         block     = get_nodes_block(n);
5956         irg       = get_irn_irg(block);
5957         bits      = get_mode_size_bits(dest_mode);
5958         tv        = new_tarval_from_long(bits-1, mode_Iu);
5959         shift_cnt = new_rd_Const(dbgi, irg, tv);
5960
5961         if (mode != dest_mode) {
5962                 a = new_rd_Conv(dbgi, block, a, dest_mode);
5963                 b = new_rd_Conv(dbgi, block, b, dest_mode);
5964         }
5965
5966         res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5967         if (need_not) {
5968                 res = new_rd_Not(dbgi, block, res, dest_mode);
5969         }
5970         res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5971         return res;
5972 }
5973
5974 /**
5975  * Optimize a Mux into some simpler cases.
5976  */
5977 static ir_node *transform_node_Mux(ir_node *n)
5978 {
5979         ir_node  *oldn = n;
5980         ir_node  *sel  = get_Mux_sel(n);
5981         ir_mode  *mode = get_irn_mode(n);
5982         ir_node  *t    = get_Mux_true(n);
5983         ir_node  *f    = get_Mux_false(n);
5984         ir_graph *irg  = get_irn_irg(n);
5985
5986         /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5987         if (get_mode_arithmetic(mode) == irma_twos_complement) {
5988                 int abs = ir_mux_is_abs(sel, f, t);
5989                 if (abs != 0) {
5990                         dbg_info *dbgi       = get_irn_dbg_info(n);
5991                         ir_node  *block      = get_nodes_block(n);
5992                         ir_node  *op         = ir_get_abs_op(sel, f, t);
5993                         int       bits       = get_mode_size_bits(mode);
5994                         ir_node  *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5995                         ir_node  *sext       = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5996                         ir_node  *xorn       = new_rd_Eor(dbgi, block, op, sext, mode);
5997                         ir_node  *res;
5998                         if (abs > 0) {
5999                                 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
6000                         } else {
6001                                 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
6002                         }
6003                         return res;
6004                 }
6005         }
6006
6007         /* first normalization step: try to move a constant to the false side,
6008          * 0 preferred on false side too */
6009         if (is_Cmp(sel) && is_Const(t) &&
6010                         (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
6011                 dbg_info *seldbgi = get_irn_dbg_info(sel);
6012                 ir_node  *block   = get_nodes_block(sel);
6013                 ir_relation relation = get_Cmp_relation(sel);
6014                 ir_node *tmp = t;
6015                 t = f;
6016                 f = tmp;
6017
6018                 /* Mux(x, a, b) => Mux(not(x), b, a) */
6019                 relation = get_negated_relation(relation);
6020                 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6021                                 get_Cmp_right(sel), relation);
6022                 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6023         }
6024
6025         if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6026                 n = transform_Mux_set(n);
6027                 if (n != oldn)
6028                         return n;
6029         }
6030
6031         /* the following optimisations create new mode_b nodes, so only do them
6032          * before mode_b lowering */
6033         if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6034                 if (is_Mux(t)) {
6035                         ir_node*  block = get_nodes_block(n);
6036                         ir_node*  c0    = sel;
6037                         ir_node*  c1    = get_Mux_sel(t);
6038                         ir_node*  t1    = get_Mux_true(t);
6039                         ir_node*  f1    = get_Mux_false(t);
6040                         if (f == f1) {
6041                                 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6042                                 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6043                                 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6044                                 return new_r_Mux(block, and_, f1, t1, mode);
6045                         } else if (f == t1) {
6046                                 /* Mux(cond0, Mux(cond1, x, y), x) */
6047                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
6048                                 ir_node* and_    = new_r_And(block, c0, not_c1, mode_b);
6049                                 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6050                                 return new_r_Mux(block, and_, t1, f1, mode);
6051                         }
6052                 } else if (is_Mux(f)) {
6053                         ir_node*  block = get_nodes_block(n);
6054                         ir_node*  c0    = sel;
6055                         ir_node*  c1    = get_Mux_sel(f);
6056                         ir_node*  t1    = get_Mux_true(f);
6057                         ir_node*  f1    = get_Mux_false(f);
6058                         if (t == t1) {
6059                                 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6060                                 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6061                                 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6062                                 return new_r_Mux(block, or_, f1, t1, mode);
6063                         } else if (t == f1) {
6064                                 /* Mux(cond0, x, Mux(cond1, y, x)) */
6065                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
6066                                 ir_node* or_     = new_r_Or(block, c0, not_c1, mode_b);
6067                                 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6068                                 return new_r_Mux(block, or_, t1, f1, mode);
6069                         }
6070                 }
6071
6072                 /* note: after normalization, false can only happen on default */
6073                 if (mode == mode_b) {
6074                         dbg_info *dbg   = get_irn_dbg_info(n);
6075                         ir_node  *block = get_nodes_block(n);
6076
6077                         if (is_Const(t)) {
6078                                 ir_tarval *tv_t = get_Const_tarval(t);
6079                                 if (tv_t == tarval_b_true) {
6080                                         if (is_Const(f)) {
6081                                                 /* Muxb(sel, true, false) = sel */
6082                                                 assert(get_Const_tarval(f) == tarval_b_false);
6083                                                 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6084                                                 return sel;
6085                                         } else {
6086                                                 /* Muxb(sel, true, x) = Or(sel, x) */
6087                                                 n = new_rd_Or(dbg, block, sel, f, mode_b);
6088                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6089                                                 return n;
6090                                         }
6091                                 }
6092                         } else if (is_Const(f)) {
6093                                 ir_tarval *tv_f = get_Const_tarval(f);
6094                                 if (tv_f == tarval_b_true) {
6095                                         /* Muxb(sel, x, true) = Or(Not(sel), x) */
6096                                         ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6097                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6098                                         n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6099                                         return n;
6100                                 } else {
6101                                         /* Muxb(sel, x, false) = And(sel, x) */
6102                                         assert(tv_f == tarval_b_false);
6103                                         n = new_rd_And(dbg, block, sel, t, mode_b);
6104                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6105                                         return n;
6106                                 }
6107                         }
6108                 }
6109         }
6110
6111         if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6112                 ir_relation relation = get_Cmp_relation(sel);
6113                 ir_node    *cmp_r    = get_Cmp_right(sel);
6114                 ir_node    *cmp_l    = get_Cmp_left(sel);
6115                 ir_node    *block    = get_nodes_block(n);
6116
6117                 if (is_And(cmp_l) && f == cmp_r) {
6118                         ir_node *and_r = get_And_right(cmp_l);
6119                         ir_node *and_l;
6120
6121                         if (and_r == t && is_single_bit(and_r)) {
6122                                 if (relation == ir_relation_equal) {
6123                                         /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6124                                         n = new_rd_Eor(get_irn_dbg_info(n),
6125                                                 block, cmp_l, t, mode);
6126                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6127                                 } else {
6128                                         /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6129                                         n = cmp_l;
6130                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6131                                 }
6132                                 return n;
6133                         }
6134                         and_l = get_And_left(cmp_l);
6135                         if (and_l == t && is_single_bit(and_l)) {
6136                                 if (relation == ir_relation_equal) {
6137                                         /* ((1 << n) & a) == 0, (1 << n), 0) */
6138                                         n = new_rd_Eor(get_irn_dbg_info(n),
6139                                                 block, cmp_l, t, mode);
6140                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6141                                 } else {
6142                                         /* ((1 << n) & a) != 0, (1 << n), 0) */
6143                                         n = cmp_l;
6144                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6145                                 }
6146                                 return n;
6147                         }
6148                 }
6149         }
6150
6151         return n;
6152 }
6153
6154 /**
6155  * optimize Sync nodes that have other syncs as input we simply add the inputs
6156  * of the other sync to our own inputs
6157  */
6158 static ir_node *transform_node_Sync(ir_node *n)
6159 {
6160         int arity = get_Sync_n_preds(n);
6161         int i;
6162
6163         for (i = 0; i < arity;) {
6164                 ir_node *pred = get_Sync_pred(n, i);
6165                 int      pred_arity;
6166                 int      j;
6167
6168                 /* Remove Bad predecessors */
6169                 if (is_Bad(pred)) {
6170                         del_Sync_n(n, i);
6171                         --arity;
6172                         continue;
6173                 }
6174
6175                 /* Remove duplicate predecessors */
6176                 for (j = 0; j < i; ++j) {
6177                         if (get_Sync_pred(n, j) == pred) {
6178                                 del_Sync_n(n, i);
6179                                 --arity;
6180                                 break;
6181                         }
6182                 }
6183                 if (j < i)
6184                         continue;
6185
6186                 if (!is_Sync(pred)) {
6187                         ++i;
6188                         continue;
6189                 }
6190
6191                 del_Sync_n(n, i);
6192                 --arity;
6193
6194                 pred_arity = get_Sync_n_preds(pred);
6195                 for (j = 0; j < pred_arity; ++j) {
6196                         ir_node *pred_pred = get_Sync_pred(pred, j);
6197                         int      k;
6198
6199                         for (k = 0;; ++k) {
6200                                 if (k >= arity) {
6201                                         add_irn_n(n, pred_pred);
6202                                         ++arity;
6203                                         break;
6204                                 }
6205                                 if (get_Sync_pred(n, k) == pred_pred) break;
6206                         }
6207                 }
6208         }
6209
6210         if (arity == 0) {
6211                 ir_graph *irg = get_irn_irg(n);
6212                 return new_r_Bad(irg, mode_M);
6213         }
6214         if (arity == 1) {
6215                 return get_Sync_pred(n, 0);
6216         }
6217
6218         /* rehash the sync node */
6219         add_identities(n);
6220         return n;
6221 }
6222
6223 static ir_node *transform_node_Load(ir_node *n)
6224 {
6225         /* if our memory predecessor is a load from the same address, then reuse the
6226          * previous result */
6227         ir_node *mem = get_Load_mem(n);
6228         ir_node *mem_pred;
6229
6230         if (!is_Proj(mem))
6231                 return n;
6232         /* don't touch volatile loads */
6233         if (get_Load_volatility(n) == volatility_is_volatile)
6234                 return n;
6235         mem_pred = get_Proj_pred(mem);
6236         if (is_Load(mem_pred)) {
6237                 ir_node *pred_load = mem_pred;
6238
6239                 /* conservatively compare the 2 loads. TODO: This could be less strict
6240                  * with fixup code in some situations (like smaller/bigger modes) */
6241                 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6242                         return n;
6243                 if (get_Load_mode(pred_load) != get_Load_mode(n))
6244                         return n;
6245                 /* all combinations of aligned/unaligned pred/n should be fine so we do
6246                  * not compare the unaligned attribute */
6247                 {
6248                         ir_node  *block = get_nodes_block(n);
6249                         ir_node  *jmp   = new_r_Jmp(block);
6250                         ir_graph *irg   = get_irn_irg(n);
6251                         ir_node  *bad   = new_r_Bad(irg, mode_X);
6252                         ir_mode  *mode  = get_Load_mode(n);
6253                         ir_node  *res   = new_r_Proj(pred_load, mode, pn_Load_res);
6254                         ir_node  *in[]  = { mem, res, jmp, bad };
6255                         ir_node  *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6256                         return tuple;
6257                 }
6258         } else if (is_Store(mem_pred)) {
6259                 ir_node *pred_store = mem_pred;
6260                 ir_node *value      = get_Store_value(pred_store);
6261
6262                 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6263                         return n;
6264                 if (get_irn_mode(value) != get_Load_mode(n))
6265                         return n;
6266                 /* all combinations of aligned/unaligned pred/n should be fine so we do
6267                  * not compare the unaligned attribute */
6268                 {
6269                         ir_node  *block = get_nodes_block(n);
6270                         ir_node  *jmp   = new_r_Jmp(block);
6271                         ir_graph *irg   = get_irn_irg(n);
6272                         ir_node  *bad   = new_r_Bad(irg, mode_X);
6273                         ir_node  *res   = value;
6274                         ir_node  *in[]  = { mem, res, jmp, bad };
6275                         ir_node  *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6276                         return tuple;
6277                 }
6278         }
6279
6280         return n;
6281 }
6282
6283 /**
6284  * optimize a trampoline Call into a direct Call
6285  */
6286 static ir_node *transform_node_Call(ir_node *call)
6287 {
6288         ir_node  *callee = get_Call_ptr(call);
6289         ir_node  *adr, *mem, *res, *bl, **in;
6290         ir_type  *ctp, *mtp, *tp;
6291         ir_graph *irg;
6292         type_dbg_info *tdb;
6293         dbg_info *db;
6294         size_t   i, n_res, n_param;
6295         ir_variadicity var;
6296
6297         if (! is_Proj(callee))
6298                 return call;
6299         callee = get_Proj_pred(callee);
6300         if (! is_Builtin(callee))
6301                 return call;
6302         if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6303                 return call;
6304
6305         mem = get_Call_mem(call);
6306
6307         if (skip_Proj(mem) == callee) {
6308                 /* memory is routed to the trampoline, skip */
6309                 mem = get_Builtin_mem(callee);
6310         }
6311
6312         /* build a new call type */
6313         mtp = get_Call_type(call);
6314         tdb = get_type_dbg_info(mtp);
6315
6316         n_res   = get_method_n_ress(mtp);
6317         n_param = get_method_n_params(mtp);
6318         ctp     = new_d_type_method(n_param + 1, n_res, tdb);
6319
6320         for (i = 0; i < n_res; ++i)
6321                 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6322
6323         NEW_ARR_A(ir_node *, in, n_param + 1);
6324
6325         /* FIXME: we don't need a new pointer type in every step */
6326         irg = get_irn_irg(call);
6327         tp = get_irg_frame_type(irg);
6328         tp = new_type_pointer(tp);
6329         set_method_param_type(ctp, 0, tp);
6330
6331         in[0] = get_Builtin_param(callee, 2);
6332         for (i = 0; i < n_param; ++i) {
6333                 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6334                 in[i + 1] = get_Call_param(call, i);
6335         }
6336         var = get_method_variadicity(mtp);
6337         set_method_variadicity(ctp, var);
6338         /* When we resolve a trampoline, the function must be called by a this-call */
6339         set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6340         set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6341
6342         adr = get_Builtin_param(callee, 1);
6343
6344         db  = get_irn_dbg_info(call);
6345         bl  = get_nodes_block(call);
6346
6347         res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6348         if (get_irn_pinned(call) == op_pin_state_floats)
6349                 set_irn_pinned(res, op_pin_state_floats);
6350         return res;
6351 }
6352
6353 /**
6354  * Sets the default transform node operation for an ir_op_ops.
6355  *
6356  * @param code   the opcode for the default operation
6357  * @param ops    the operations initialized
6358  *
6359  * @return
6360  *    The operations.
6361  */
6362 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6363 {
6364 #define CASE(a)                                         \
6365         case iro_##a:                                       \
6366                 ops->transform_node      = transform_node_##a;  \
6367                 break
6368 #define CASE_PROJ(a)                                         \
6369         case iro_##a:                                            \
6370                 ops->transform_node_Proj = transform_node_Proj_##a;  \
6371                 break
6372 #define CASE_PROJ_EX(a)                                      \
6373         case iro_##a:                                            \
6374                 ops->transform_node      = transform_node_##a;       \
6375                 ops->transform_node_Proj = transform_node_Proj_##a;  \
6376                 break
6377
6378         switch (code) {
6379         CASE(Add);
6380         CASE(And);
6381         CASE(Block);
6382         CASE(Call);
6383         CASE(Cmp);
6384         CASE(Cond);
6385         CASE(Conv);
6386         CASE(End);
6387         CASE(Eor);
6388         CASE(Minus);
6389         CASE(Mul);
6390         CASE(Mux);
6391         CASE(Not);
6392         CASE(Or);
6393         CASE(Phi);
6394         CASE(Proj);
6395         CASE(Rotl);
6396         CASE(Sel);
6397         CASE(Shl);
6398         CASE(Shr);
6399         CASE(Shrs);
6400         CASE(Sub);
6401         CASE(Switch);
6402         CASE(Sync);
6403         CASE_PROJ(Bound);
6404         CASE_PROJ(CopyB);
6405         CASE_PROJ(Store);
6406         CASE_PROJ_EX(Div);
6407         CASE_PROJ_EX(Load);
6408         CASE_PROJ_EX(Mod);
6409         default:
6410                 break;
6411         }
6412
6413         return ops;
6414 #undef CASE_PROJ_EX
6415 #undef CASE_PROJ
6416 #undef CASE
6417 }
6418
6419 /**
6420  * Tries several [inplace] [optimizing] transformations and returns an
6421  * equivalent node.  The difference to equivalent_node() is that these
6422  * transformations _do_ generate new nodes, and thus the old node must
6423  * not be freed even if the equivalent node isn't the old one.
6424  */
6425 static ir_node *transform_node(ir_node *n)
6426 {
6427         ir_node *old_n;
6428         unsigned iro;
6429 restart:
6430         old_n = n;
6431         iro   = get_irn_opcode_(n);
6432         /* constant expression evaluation / constant folding */
6433         if (get_opt_constant_folding()) {
6434                 /* neither constants nor Tuple values can be evaluated */
6435                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6436                         /* try to evaluate */
6437                         ir_tarval *tv = computed_value(n);
6438                         if (tv != tarval_bad) {
6439                                 /* evaluation was successful -- replace the node. */
6440                                 ir_graph *irg = get_irn_irg(n);
6441
6442                                 n = new_r_Const(irg, tv);
6443
6444                                 DBG_OPT_CSTEVAL(old_n, n);
6445                                 return n;
6446                         }
6447                 }
6448         }
6449
6450         /* remove unnecessary nodes */
6451         if (get_opt_constant_folding() ||
6452                 (iro == iro_Phi)  ||   /* always optimize these nodes. */
6453                 (iro == iro_Id)   ||   /* ... */
6454                 (iro == iro_Proj) ||   /* ... */
6455                 (iro == iro_Block)) {  /* Flags tested local. */
6456                 n = equivalent_node(n);
6457                 if (n != old_n)
6458                         goto restart;
6459         }
6460
6461         /* Some more constant expression evaluation. */
6462         if (get_opt_algebraic_simplification() ||
6463                 (iro == iro_Cond) ||
6464                 (iro == iro_Proj)) {    /* Flags tested local. */
6465                 if (n->op->ops.transform_node != NULL) {
6466                         n = n->op->ops.transform_node(n);
6467                         if (n != old_n) {
6468                                 goto restart;
6469                         }
6470                 }
6471         }
6472
6473         return n;
6474 }
6475
6476 /* **************** Common Subexpression Elimination **************** */
6477
6478 /** The size of the hash table used, should estimate the number of nodes
6479     in a graph. */
6480 #define N_IR_NODES 512
6481
6482 /** Compares two exception attributes */
6483 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6484 {
6485         const except_attr *ea = &a->attr.except;
6486         const except_attr *eb = &b->attr.except;
6487         return ea->pin_state != eb->pin_state;
6488 }
6489
6490 /** Compares the attributes of two Const nodes. */
6491 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6492 {
6493         return get_Const_tarval(a) != get_Const_tarval(b);
6494 }
6495
6496 /** Compares the attributes of two Proj nodes. */
6497 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6498 {
6499         return a->attr.proj.proj != b->attr.proj.proj;
6500 }
6501
6502 /** Compares the attributes of two Alloc nodes. */
6503 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6504 {
6505         const alloc_attr *pa = &a->attr.alloc;
6506         const alloc_attr *pb = &b->attr.alloc;
6507         if (pa->where != pb->where || pa->type != pb->type)
6508                 return 1;
6509         return node_cmp_exception(a, b);
6510 }
6511
6512 /** Compares the attributes of two Free nodes. */
6513 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6514 {
6515         const free_attr *pa = &a->attr.free;
6516         const free_attr *pb = &b->attr.free;
6517         return (pa->where != pb->where) || (pa->type != pb->type);
6518 }
6519
6520 /** Compares the attributes of two SymConst nodes. */
6521 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6522 {
6523         const symconst_attr *pa = &a->attr.symc;
6524         const symconst_attr *pb = &b->attr.symc;
6525         return (pa->kind       != pb->kind)
6526             || (pa->sym.type_p != pb->sym.type_p);
6527 }
6528
6529 /** Compares the attributes of two Call nodes. */
6530 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6531 {
6532         const call_attr *pa = &a->attr.call;
6533         const call_attr *pb = &b->attr.call;
6534         if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6535                 return 1;
6536         return node_cmp_exception(a, b);
6537 }
6538
6539 /** Compares the attributes of two Sel nodes. */
6540 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6541 {
6542         const ir_entity *a_ent = get_Sel_entity(a);
6543         const ir_entity *b_ent = get_Sel_entity(b);
6544         return a_ent != b_ent;
6545 }
6546
6547 /** Compares the attributes of two Phi nodes. */
6548 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6549 {
6550         /* we can only enter this function if both nodes have the same number of inputs,
6551            hence it is enough to check if one of them is a Phi0 */
6552         if (is_Phi0(a)) {
6553                 /* check the Phi0 pos attribute */
6554                 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6555         }
6556         return 0;
6557 }
6558
6559 /** Compares the attributes of two Conv nodes. */
6560 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6561 {
6562         return get_Conv_strict(a) != get_Conv_strict(b);
6563 }
6564
6565 /** Compares the attributes of two Cast nodes. */
6566 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6567 {
6568         return get_Cast_type(a) != get_Cast_type(b);
6569 }
6570
6571 /** Compares the attributes of two Load nodes. */
6572 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6573 {
6574         if (get_Load_volatility(a) == volatility_is_volatile ||
6575             get_Load_volatility(b) == volatility_is_volatile)
6576                 /* NEVER do CSE on volatile Loads */
6577                 return 1;
6578         /* do not CSE Loads with different alignment. Be conservative. */
6579         if (get_Load_unaligned(a) != get_Load_unaligned(b))
6580                 return 1;
6581         if (get_Load_mode(a) != get_Load_mode(b))
6582                 return 1;
6583         return node_cmp_exception(a, b);
6584 }
6585
6586 /** Compares the attributes of two Store nodes. */
6587 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6588 {
6589         /* do not CSE Stores with different alignment. Be conservative. */
6590         if (get_Store_unaligned(a) != get_Store_unaligned(b))
6591                 return 1;
6592         /* NEVER do CSE on volatile Stores */
6593         if (get_Store_volatility(a) == volatility_is_volatile ||
6594             get_Store_volatility(b) == volatility_is_volatile)
6595                 return 1;
6596         return node_cmp_exception(a, b);
6597 }
6598
6599 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6600 {
6601         if (get_CopyB_type(a) != get_CopyB_type(b))
6602                 return 1;
6603
6604         return node_cmp_exception(a, b);
6605 }
6606
6607 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6608 {
6609         return node_cmp_exception(a, b);
6610 }
6611
6612 /** Compares the attributes of two Div nodes. */
6613 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6614 {
6615         const div_attr *ma = &a->attr.div;
6616         const div_attr *mb = &b->attr.div;
6617         if (ma->resmode != mb->resmode || ma->no_remainder  != mb->no_remainder)
6618                 return 1;
6619         return node_cmp_exception(a, b);
6620 }
6621
6622 /** Compares the attributes of two Mod nodes. */
6623 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6624 {
6625         const mod_attr *ma = &a->attr.mod;
6626         const mod_attr *mb = &b->attr.mod;
6627         if (ma->resmode != mb->resmode)
6628                 return 1;
6629         return node_cmp_exception(a, b);
6630 }
6631
6632 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6633 {
6634         const cmp_attr *ma = &a->attr.cmp;
6635         const cmp_attr *mb = &b->attr.cmp;
6636         return ma->relation != mb->relation;
6637 }
6638
6639 /** Compares the attributes of two Confirm nodes. */
6640 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6641 {
6642         const confirm_attr *ma = &a->attr.confirm;
6643         const confirm_attr *mb = &b->attr.confirm;
6644         return ma->relation != mb->relation;
6645 }
6646
6647 /** Compares the attributes of two Builtin nodes. */
6648 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6649 {
6650         if (get_Builtin_kind(a) != get_Builtin_kind(b))
6651                 return 1;
6652         if (get_Builtin_type(a) != get_Builtin_type(b))
6653                 return 1;
6654         return node_cmp_exception(a, b);
6655 }
6656
6657 /** Compares the attributes of two ASM nodes. */
6658 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6659 {
6660         int i, n;
6661         const ir_asm_constraint *ca;
6662         const ir_asm_constraint *cb;
6663         ident **cla, **clb;
6664
6665         if (get_ASM_text(a) != get_ASM_text(b))
6666                 return 1;
6667
6668         /* Should we really check the constraints here? Should be better, but is strange. */
6669         n = get_ASM_n_input_constraints(a);
6670         if (n != get_ASM_n_input_constraints(b))
6671                 return 1;
6672
6673         ca = get_ASM_input_constraints(a);
6674         cb = get_ASM_input_constraints(b);
6675         for (i = 0; i < n; ++i) {
6676                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6677                     || ca[i].mode != cb[i].mode)
6678                         return 1;
6679         }
6680
6681         n = get_ASM_n_output_constraints(a);
6682         if (n != get_ASM_n_output_constraints(b))
6683                 return 1;
6684
6685         ca = get_ASM_output_constraints(a);
6686         cb = get_ASM_output_constraints(b);
6687         for (i = 0; i < n; ++i) {
6688                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6689                     || ca[i].mode != cb[i].mode)
6690                         return 1;
6691         }
6692
6693         n = get_ASM_n_clobbers(a);
6694         if (n != get_ASM_n_clobbers(b))
6695                 return 1;
6696
6697         cla = get_ASM_clobbers(a);
6698         clb = get_ASM_clobbers(b);
6699         for (i = 0; i < n; ++i) {
6700                 if (cla[i] != clb[i])
6701                         return 1;
6702         }
6703
6704         return node_cmp_exception(a, b);
6705 }
6706
6707 /** Compares the inexistent attributes of two Dummy nodes. */
6708 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6709 {
6710         (void) a;
6711         (void) b;
6712         /* Dummy nodes never equal by definition */
6713         return 1;
6714 }
6715
6716 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6717 {
6718         if (get_InstOf_type(a) != get_InstOf_type(b))
6719                 return 1;
6720         return node_cmp_exception(a, b);
6721 }
6722
6723 /**
6724  * Set the default node attribute compare operation for an ir_op_ops.
6725  *
6726  * @param code   the opcode for the default operation
6727  * @param ops    the operations initialized
6728  *
6729  * @return
6730  *    The operations.
6731  */
6732 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6733 {
6734 #define CASE(a)                              \
6735         case iro_##a:                              \
6736                 ops->node_cmp_attr  = node_cmp_attr_##a; \
6737                 break
6738
6739         switch (code) {
6740         CASE(ASM);
6741         CASE(Alloc);
6742         CASE(Bound);
6743         CASE(Builtin);
6744         CASE(Call);
6745         CASE(Cast);
6746         CASE(Cmp);
6747         CASE(Confirm);
6748         CASE(Const);
6749         CASE(Conv);
6750         CASE(CopyB);
6751         CASE(Div);
6752         CASE(Dummy);
6753         CASE(Free);
6754         CASE(InstOf);
6755         CASE(Load);
6756         CASE(Mod);
6757         CASE(Phi);
6758         CASE(Proj);
6759         CASE(Sel);
6760         CASE(Store);
6761         CASE(SymConst);
6762         default:
6763                 /* leave NULL */
6764                 break;
6765         }
6766
6767         return ops;
6768 #undef CASE
6769 }
6770
6771 /*
6772  * Compare function for two nodes in the value table. Gets two
6773  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
6774  */
6775 int identities_cmp(const void *elt, const void *key)
6776 {
6777         ir_node *a = (ir_node *)elt;
6778         ir_node *b = (ir_node *)key;
6779         int i, irn_arity_a;
6780
6781         if (a == b) return 0;
6782
6783         if ((get_irn_op(a) != get_irn_op(b)) ||
6784             (get_irn_mode(a) != get_irn_mode(b))) return 1;
6785
6786         /* compare if a's in and b's in are of equal length */
6787         irn_arity_a = get_irn_arity(a);
6788         if (irn_arity_a != get_irn_arity(b))
6789                 return 1;
6790
6791         /* blocks are never the same */
6792         if (is_Block(a))
6793                 return 1;
6794
6795         if (get_irn_pinned(a) == op_pin_state_pinned) {
6796                 /* for pinned nodes, the block inputs must be equal */
6797                 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6798                         return 1;
6799         } else {
6800                 ir_node *block_a = get_nodes_block(a);
6801                 ir_node *block_b = get_nodes_block(b);
6802                 if (! get_opt_global_cse()) {
6803                         /* for block-local CSE both nodes must be in the same Block */
6804                         if (block_a != block_b)
6805                                 return 1;
6806                 } else {
6807                         /* The optimistic approach would be to do nothing here.
6808                          * However doing GCSE optimistically produces a lot of partially dead code which appears
6809                          * to be worse in practice than the missed opportunities.
6810                          * So we use a very conservative variant here and only CSE if 1 value dominates the
6811                          * other. */
6812                         if (!block_dominates(block_a, block_b)
6813                             && !block_dominates(block_b, block_a))
6814                             return 1;
6815                 }
6816         }
6817
6818         /* compare a->in[0..ins] with b->in[0..ins] */
6819         for (i = 0; i < irn_arity_a; ++i) {
6820                 ir_node *pred_a = get_irn_n(a, i);
6821                 ir_node *pred_b = get_irn_n(b, i);
6822                 if (pred_a != pred_b) {
6823                         /* if both predecessors are CSE neutral they might be different */
6824                         if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6825                                 return 1;
6826                 }
6827         }
6828
6829         /*
6830          * here, we already now that the nodes are identical except their
6831          * attributes
6832          */
6833         if (a->op->ops.node_cmp_attr)
6834                 return a->op->ops.node_cmp_attr(a, b);
6835
6836         return 0;
6837 }
6838
6839 /*
6840  * Calculate a hash value of a node.
6841  *
6842  * @param node  The IR-node
6843  */
6844 unsigned ir_node_hash(const ir_node *node)
6845 {
6846         return node->op->ops.hash(node);
6847 }
6848
6849
6850 void new_identities(ir_graph *irg)
6851 {
6852         if (irg->value_table != NULL)
6853                 del_pset(irg->value_table);
6854         irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6855 }
6856
6857 void del_identities(ir_graph *irg)
6858 {
6859         if (irg->value_table != NULL)
6860                 del_pset(irg->value_table);
6861 }
6862
6863 /* Normalize a node by putting constants (and operands with larger
6864  * node index) on the right (operator side). */
6865 void ir_normalize_node(ir_node *n)
6866 {
6867         if (is_op_commutative(get_irn_op(n))) {
6868                 ir_node *l = get_binop_left(n);
6869                 ir_node *r = get_binop_right(n);
6870
6871                 /* For commutative operators perform  a OP b == b OP a but keep
6872                  * constants on the RIGHT side. This helps greatly in some
6873                  * optimizations.  Moreover we use the idx number to make the form
6874                  * deterministic. */
6875                 if (!operands_are_normalized(l, r)) {
6876                         set_binop_left(n, r);
6877                         set_binop_right(n, l);
6878                         hook_normalize(n);
6879                 }
6880         }
6881 }
6882
6883 /*
6884  * Return the canonical node computing the same value as n.
6885  * Looks up the node in a hash table, enters it in the table
6886  * if it isn't there yet.
6887  *
6888  * @param n            the node to look up
6889  *
6890  * @return a node that computes the same value as n or n if no such
6891  *         node could be found
6892  */
6893 ir_node *identify_remember(ir_node *n)
6894 {
6895         ir_graph *irg         = get_irn_irg(n);
6896         pset     *value_table = irg->value_table;
6897         ir_node  *nn;
6898
6899         if (value_table == NULL)
6900                 return n;
6901
6902         ir_normalize_node(n);
6903         /* lookup or insert in hash table with given hash key. */
6904         nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6905
6906         if (nn != n) {
6907                 /* n is reachable again */
6908                 edges_node_revival(nn);
6909         }
6910
6911         return nn;
6912 }
6913
6914 /**
6915  * During construction we set the op_pin_state_pinned flag in the graph right
6916  * when the optimization is performed.  The flag turning on procedure global
6917  * cse could be changed between two allocations.  This way we are safe.
6918  *
6919  * @param n            The node to lookup
6920  */
6921 static inline ir_node *identify_cons(ir_node *n)
6922 {
6923         ir_node *old = n;
6924
6925         n = identify_remember(n);
6926         if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6927                 ir_graph *irg = get_irn_irg(n);
6928                 set_irg_pinned(irg, op_pin_state_floats);
6929         }
6930         return n;
6931 }
6932
6933 /* Add a node to the identities value table. */
6934 void add_identities(ir_node *node)
6935 {
6936         if (!get_opt_cse())
6937                 return;
6938         if (is_Block(node))
6939                 return;
6940
6941         identify_remember(node);
6942 }
6943
6944 /* Visit each node in the value table of a graph. */
6945 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6946 {
6947         ir_node  *node;
6948         ir_graph *rem = current_ir_graph;
6949
6950         current_ir_graph = irg;
6951         foreach_pset(irg->value_table, ir_node*, node) {
6952                 visit(node, env);
6953         }
6954         current_ir_graph = rem;
6955 }
6956
6957 /**
6958  * These optimizations deallocate nodes from the obstack.
6959  * It can only be called if it is guaranteed that no other nodes
6960  * reference this one, i.e., right after construction of a node.
6961  *
6962  * @param n   The node to optimize
6963  */
6964 ir_node *optimize_node(ir_node *n)
6965 {
6966         ir_node   *oldn = n;
6967         ir_graph  *irg  = get_irn_irg(n);
6968         unsigned   iro  = get_irn_opcode(n);
6969         ir_tarval *tv;
6970
6971         /* Always optimize Phi nodes: part of the construction. */
6972         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6973
6974         /* constant expression evaluation / constant folding */
6975         if (get_opt_constant_folding()) {
6976                 /* neither constants nor Tuple values can be evaluated */
6977                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6978                         /* try to evaluate */
6979                         tv = computed_value(n);
6980                         if (tv != tarval_bad) {
6981                                 ir_node *nw;
6982                                 size_t node_size;
6983
6984                                 /*
6985                                  * we MUST copy the node here temporarily, because it's still
6986                                  * needed for DBG_OPT_CSTEVAL
6987                                  */
6988                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
6989                                 oldn = (ir_node*)alloca(node_size);
6990
6991                                 memcpy(oldn, n, node_size);
6992                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6993
6994                                 /* ARG, copy the in array, we need it for statistics */
6995                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6996
6997                                 /* note the inplace edges module */
6998                                 edges_node_deleted(n);
6999
7000                                 /* evaluation was successful -- replace the node. */
7001                                 irg_kill_node(irg, n);
7002                                 nw = new_r_Const(irg, tv);
7003
7004                                 DBG_OPT_CSTEVAL(oldn, nw);
7005                                 return nw;
7006                         }
7007                 }
7008         }
7009
7010         /* remove unnecessary nodes */
7011         if (get_opt_algebraic_simplification() ||
7012             (iro == iro_Phi)  ||   /* always optimize these nodes. */
7013             (iro == iro_Id)   ||
7014             (iro == iro_Proj) ||
7015             (iro == iro_Block)  )  /* Flags tested local. */
7016                 n = equivalent_node(n);
7017
7018         /* Common Subexpression Elimination.
7019          *
7020          * Checks whether n is already available.
7021          * The block input is used to distinguish different subexpressions. Right
7022          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
7023          * subexpressions within a block.
7024          */
7025         if (get_opt_cse())
7026                 n = identify_cons(n);
7027
7028         if (n != oldn) {
7029                 edges_node_deleted(oldn);
7030
7031                 /* We found an existing, better node, so we can deallocate the old node. */
7032                 irg_kill_node(irg, oldn);
7033                 return n;
7034         }
7035
7036         /* Some more constant expression evaluation that does not allow to
7037            free the node. */
7038         iro = get_irn_opcode(n);
7039         if (get_opt_algebraic_simplification() ||
7040                 (iro == iro_Cond) ||
7041                 (iro == iro_Proj)) {    /* Flags tested local. */
7042                 n = transform_node(n);
7043         }
7044
7045         /* Now we have a legal, useful node. Enter it in hash table for CSE */
7046         if (get_opt_cse()) {
7047                 ir_node *o = n;
7048                 n = identify_remember(o);
7049                 if (o != n)
7050                         DBG_OPT_CSE(o, n);
7051         }
7052
7053         return n;
7054 }
7055
7056
7057 /**
7058  * These optimizations never deallocate nodes (in place).  This can cause dead
7059  * nodes lying on the obstack.  Remove these by a dead node elimination,
7060  * i.e., a copying garbage collection.
7061  */
7062 ir_node *optimize_in_place_2(ir_node *n)
7063 {
7064         if (!get_opt_optimize() && !is_Phi(n)) return n;
7065
7066         if (is_Deleted(n))
7067                 return n;
7068
7069         /** common subexpression elimination **/
7070         /* Checks whether n is already available. */
7071         /* The block input is used to distinguish different subexpressions.
7072          * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
7073          * only finds common subexpressions within a block. */
7074         if (get_opt_cse()) {
7075                 ir_node *o = n;
7076                 n = identify_remember(n);
7077                 if (n != o) {
7078                         DBG_OPT_CSE(o, n);
7079                         /* we have another existing node now, we do not optimize it here */
7080                         return n;
7081                 }
7082         }
7083
7084         n = transform_node(n);
7085
7086         /* Now we can verify the node, as it has no dead inputs any more. */
7087         irn_verify(n);
7088
7089         /* Now we have a legal, useful node. Enter it in hash table for cse.
7090          * Blocks should be unique anyways.  (Except the successor of start:
7091          * is cse with the start block!)
7092          *
7093          * Note: This is only necessary because some of the optimisations
7094          * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7095          * bad practice and should be fixed sometime.
7096          */
7097         if (get_opt_cse()) {
7098                 ir_node *o = n;
7099                 n = identify_remember(o);
7100                 if (o != n)
7101                         DBG_OPT_CSE(o, n);
7102         }
7103
7104         return n;
7105 }
7106
7107 /**
7108  * Wrapper for external use, set proper status bits after optimization.
7109  */
7110 ir_node *optimize_in_place(ir_node *n)
7111 {
7112         ir_graph *irg = get_irn_irg(n);
7113         /* Handle graph state */
7114         assert(get_irg_phase_state(irg) != phase_building);
7115
7116         if (get_opt_global_cse())
7117                 set_irg_pinned(irg, op_pin_state_floats);
7118
7119         /* FIXME: Maybe we could also test whether optimizing the node can
7120            change the control graph. */
7121         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7122         return optimize_in_place_2(n);
7123 }
7124
7125 /**
7126  * Calculate a hash value of a Const node.
7127  */
7128 static unsigned hash_Const(const ir_node *node)
7129 {
7130         unsigned h;
7131
7132         /* special value for const, as they only differ in their tarval. */
7133         h = HASH_PTR(node->attr.con.tarval);
7134
7135         return h;
7136 }
7137
7138 /**
7139  * Calculate a hash value of a SymConst node.
7140  */
7141 static unsigned hash_SymConst(const ir_node *node)
7142 {
7143         unsigned h;
7144
7145         /* all others are pointers */
7146         h = HASH_PTR(node->attr.symc.sym.type_p);
7147
7148         return h;
7149 }
7150
7151 /**
7152  * Set the default hash operation in an ir_op_ops.
7153  *
7154  * @param code   the opcode for the default operation
7155  * @param ops    the operations initialized
7156  *
7157  * @return
7158  *    The operations.
7159  */
7160 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
7161 {
7162 #define CASE(a)                                    \
7163         case iro_##a:                                  \
7164                 ops->hash  = hash_##a; \
7165                 break
7166
7167         /* hash function already set */
7168         if (ops->hash != NULL)
7169                 return ops;
7170
7171         switch (code) {
7172         CASE(Const);
7173         CASE(SymConst);
7174         default:
7175                 /* use input/mode default hash if no function was given */
7176                 ops->hash = firm_default_hash;
7177         }
7178
7179         return ops;
7180 #undef CASE
7181 }
7182
7183 /*
7184  * Sets the default operation for an ir_ops.
7185  */
7186 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
7187 {
7188         ops = firm_set_default_hash(code, ops);
7189         ops = firm_set_default_computed_value(code, ops);
7190         ops = firm_set_default_equivalent_node(code, ops);
7191         ops = firm_set_default_transform_node(code, ops);
7192         ops = firm_set_default_node_cmp_attr(code, ops);
7193         ops = firm_set_default_get_type_attr(code, ops);
7194         ops = firm_set_default_get_entity_attr(code, ops);
7195
7196         return ops;
7197 }