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