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