verify: Clarify assertion message.
[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         block     = get_nodes_block(n);
2080         dbg_bitop = get_irn_dbg_info(n);
2081         dbg_shift = get_irn_dbg_info(left);
2082         tv1       = get_Const_tarval(shift_right);
2083         tv2       = get_Const_tarval(right);
2084         assert(get_tarval_mode(tv2) == mode);
2085
2086         if (is_Shl(left)) {
2087                 tv_bitop = tarval_shr(tv2, tv1);
2088
2089                 /* Check whether we have lost some bits during the right shift. */
2090                 if (!is_And(n)) {
2091                         ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2092
2093                         if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2094                                 return n;
2095                 }
2096         } else if (is_Shr(left)) {
2097                 if (!is_And(n)) {
2098                         /*
2099                          * TODO this can be improved by checking whether
2100                          *      the left shift produces an overflow
2101                          */
2102                         return n;
2103                 }
2104                 tv_bitop = tarval_shl(tv2, tv1);
2105         } else {
2106                 assert(is_Rotl(left));
2107                 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2108         }
2109         new_const = new_r_Const(irg, tv_bitop);
2110
2111         if (is_And(n)) {
2112                 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2113         } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2114                 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2115         } else {
2116                 assert(is_Eor(n));
2117                 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2118         }
2119
2120         if (is_Shl(left)) {
2121                 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2122         } else if (is_Shr(left)) {
2123                 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2124         } else {
2125                 assert(is_Rotl(left));
2126                 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2127         }
2128
2129         return new_shift;
2130 }
2131
2132 static bool complement_values(const ir_node *a, const ir_node *b)
2133 {
2134         if (is_Not(a) && get_Not_op(a) == b)
2135                 return true;
2136         if (is_Not(b) && get_Not_op(b) == a)
2137                 return true;
2138         if (is_Const(a) && is_Const(b)) {
2139                 ir_tarval *tv_a = get_Const_tarval(a);
2140                 ir_tarval *tv_b = get_Const_tarval(b);
2141                 return tarval_not(tv_a) == tv_b;
2142         }
2143         return false;
2144 }
2145
2146 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2147
2148 /**
2149  * for associative operations fold:
2150  *   op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2151  * This is a "light" version of the reassociation phase
2152  */
2153 static ir_node *fold_constant_associativity(ir_node *node,
2154                                             tv_fold_binop_func fold)
2155 {
2156         ir_graph  *irg;
2157         ir_op     *op;
2158         ir_node   *left;
2159         ir_node   *right = get_binop_right(node);
2160         ir_node   *left_right;
2161         ir_node   *left_left;
2162         ir_tarval *c0;
2163         ir_tarval *c1;
2164         ir_tarval *new_c;
2165         ir_node   *new_const;
2166         ir_node   *new_node;
2167         if (!is_Const(right))
2168                 return node;
2169
2170         op   = get_irn_op(node);
2171         left = get_binop_left(node);
2172         if (get_irn_op(left) != op)
2173                 return node;
2174
2175         left_right = get_binop_right(left);
2176         if (!is_Const(left_right))
2177                 return node;
2178
2179         left_left = get_binop_left(left);
2180         c0        = get_Const_tarval(left_right);
2181         c1        = get_Const_tarval(right);
2182         irg       = get_irn_irg(node);
2183         if (get_tarval_mode(c0) != get_tarval_mode(c1))
2184                 return node;
2185         new_c     = fold(c0, c1);
2186         if (new_c == tarval_bad)
2187                 return node;
2188         new_const = new_r_Const(irg, new_c);
2189         new_node  = exact_copy(node);
2190         set_binop_left(new_node, left_left);
2191         set_binop_right(new_node, new_const);
2192         return new_node;
2193 }
2194
2195 /**
2196  * Transform an Or.
2197  */
2198 static ir_node *transform_node_Or_(ir_node *n)
2199 {
2200         ir_node *oldn = n;
2201         ir_node *a    = get_binop_left(n);
2202         ir_node *b    = get_binop_right(n);
2203         ir_node *c;
2204         ir_mode *mode;
2205
2206         n = fold_constant_associativity(n, tarval_or);
2207         if (n != oldn)
2208                 return n;
2209
2210         if (is_Not(a) && is_Not(b)) {
2211                 /* ~a | ~b = ~(a&b) */
2212                 ir_node *block = get_nodes_block(n);
2213
2214                 mode = get_irn_mode(n);
2215                 a = get_Not_op(a);
2216                 b = get_Not_op(b);
2217                 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2218                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2219                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2220                 return n;
2221         }
2222
2223         /* we can combine the relations of two compares with the same operands */
2224         if (is_Cmp(a) && is_Cmp(b)) {
2225                 ir_node *a_left  = get_Cmp_left(a);
2226                 ir_node *a_right = get_Cmp_right(a);
2227                 ir_node *b_left  = get_Cmp_left(b);
2228                 ir_node *b_right = get_Cmp_right(b);
2229                 if (a_left == b_left && b_left == b_right) {
2230                         dbg_info   *dbgi         = get_irn_dbg_info(n);
2231                         ir_node    *block        = get_nodes_block(n);
2232                         ir_relation a_relation   = get_Cmp_relation(a);
2233                         ir_relation b_relation   = get_Cmp_relation(b);
2234                         ir_relation new_relation = a_relation | b_relation;
2235                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2236                 }
2237                 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2238                 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2239                     && !mode_is_float(get_irn_mode(a_left))
2240                     && !mode_is_float(get_irn_mode(b_left))) {
2241                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2242                                 ir_graph *irg    = get_irn_irg(n);
2243                                 dbg_info *dbgi   = get_irn_dbg_info(n);
2244                                 ir_node  *block  = get_nodes_block(n);
2245                                 ir_mode  *a_mode = get_irn_mode(a_left);
2246                                 ir_mode  *b_mode = get_irn_mode(b_left);
2247                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2248                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2249                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
2250                                 ir_node  *orn    = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2251                                 ir_node  *zero   = create_zero_const(irg, b_mode);
2252                                 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2253                         }
2254                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2255                                 ir_graph *irg    = get_irn_irg(n);
2256                                 dbg_info *dbgi   = get_irn_dbg_info(n);
2257                                 ir_node  *block  = get_nodes_block(n);
2258                                 ir_mode  *a_mode = get_irn_mode(a_left);
2259                                 ir_mode  *b_mode = get_irn_mode(b_left);
2260                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2261                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2262                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
2263                                 ir_node  *orn    = new_rd_Or(dbgi, block, xora, conv, a_mode);
2264                                 ir_node  *zero   = create_zero_const(irg, a_mode);
2265                                 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2266                         }
2267                 }
2268         }
2269
2270         mode = get_irn_mode(n);
2271         HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2272
2273         n = transform_node_Or_bf_store(n);
2274         if (n != oldn)
2275                 return n;
2276         n = transform_node_Or_Rotl(n);
2277         if (n != oldn)
2278                 return n;
2279
2280         n = transform_bitwise_distributive(n, transform_node_Or_);
2281         if (n != oldn)
2282                 return n;
2283         n = transform_node_bitop_shift(n);
2284         if (n != oldn)
2285                 return n;
2286
2287         return n;
2288 }
2289
2290 static ir_node *transform_node_Or(ir_node *n)
2291 {
2292         if (is_Or_Eor_Add(n)) {
2293                 dbg_info *dbgi  = get_irn_dbg_info(n);
2294                 ir_node  *block = get_nodes_block(n);
2295                 ir_node  *left  = get_Or_left(n);
2296                 ir_node  *right = get_Or_right(n);
2297                 ir_mode  *mode  = get_irn_mode(n);
2298                 return new_rd_Add(dbgi, block, left, right, mode);
2299         }
2300         return transform_node_Or_(n);
2301 }
2302
2303 /**
2304  * Transform an Eor.
2305  */
2306 static ir_node *transform_node_Eor_(ir_node *n)
2307 {
2308         ir_node *oldn = n;
2309         ir_node *a    = get_binop_left(n);
2310         ir_node *b    = get_binop_right(n);
2311         ir_mode *mode = get_irn_mode(n);
2312         ir_node *c;
2313
2314         n = fold_constant_associativity(n, tarval_eor);
2315         if (n != oldn)
2316                 return n;
2317
2318         /* we can combine the relations of two compares with the same operands */
2319         if (is_Cmp(a) && is_Cmp(b)) {
2320                 ir_node *a_left  = get_Cmp_left(a);
2321                 ir_node *a_right = get_Cmp_left(a);
2322                 ir_node *b_left  = get_Cmp_left(b);
2323                 ir_node *b_right = get_Cmp_right(b);
2324                 if (a_left == b_left && b_left == b_right) {
2325                         dbg_info   *dbgi         = get_irn_dbg_info(n);
2326                         ir_node    *block        = get_nodes_block(n);
2327                         ir_relation a_relation   = get_Cmp_relation(a);
2328                         ir_relation b_relation   = get_Cmp_relation(b);
2329                         ir_relation new_relation = a_relation ^ b_relation;
2330                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2331                 }
2332         }
2333
2334         HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2335
2336         /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2337         if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2338                 dbg_info *dbg      = get_irn_dbg_info(n);
2339                 ir_node  *block    = get_nodes_block(n);
2340                 ir_node  *new_not  = new_rd_Not(dbg, block, b, mode);
2341                 ir_node  *new_left = get_Not_op(a);
2342                 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2343                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2344                 return n;
2345         } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2346                 dbg_info *dbg       = get_irn_dbg_info(n);
2347                 ir_node  *block     = get_nodes_block(n);
2348                 ir_node  *new_not   = new_rd_Not(dbg, block, a, mode);
2349                 ir_node  *new_right = get_Not_op(b);
2350                 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2351                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2352                 return n;
2353         }
2354
2355         /* x ^ 1...1 -> ~1 */
2356         if (is_Const(b) && is_Const_all_one(b)) {
2357                 n = new_r_Not(get_nodes_block(n), a, mode);
2358                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2359                 return n;
2360         }
2361
2362         n = transform_bitwise_distributive(n, transform_node_Eor_);
2363         if (n != oldn)
2364                 return n;
2365         n = transform_node_bitop_shift(n);
2366         if (n != oldn)
2367                 return n;
2368
2369         return n;
2370 }
2371
2372 static ir_node *transform_node_Eor(ir_node *n)
2373 {
2374         if (is_Or_Eor_Add(n)) {
2375                 dbg_info *dbgi  = get_irn_dbg_info(n);
2376                 ir_node  *block = get_nodes_block(n);
2377                 ir_node  *left  = get_Eor_left(n);
2378                 ir_node  *right = get_Eor_right(n);
2379                 ir_mode  *mode  = get_irn_mode(n);
2380                 return new_rd_Add(dbgi, block, left, right, mode);
2381         }
2382         return transform_node_Eor_(n);
2383 }
2384
2385 /**
2386  * Do the AddSub optimization, then Transform
2387  *   Constant folding on Phi
2388  *   Add(a,a)          -> Mul(a, 2)
2389  *   Add(Mul(a, x), a) -> Mul(a, x+1)
2390  * if the mode is integer or float.
2391  * Transform Add(a,-b) into Sub(a,b).
2392  * Reassociation might fold this further.
2393  */
2394 static ir_node *transform_node_Add(ir_node *n)
2395 {
2396         ir_mode *mode;
2397         ir_node *a;
2398         ir_node *b;
2399         ir_node *c;
2400         ir_node *oldn = n;
2401
2402         n = fold_constant_associativity(n, tarval_add);
2403         if (n != oldn)
2404                 return n;
2405
2406         n = transform_node_AddSub(n);
2407         if (n != oldn)
2408                 return n;
2409
2410         a    = get_Add_left(n);
2411         b    = get_Add_right(n);
2412         mode = get_irn_mode(n);
2413
2414         if (mode_is_reference(mode)) {
2415                 ir_mode *lmode = get_irn_mode(a);
2416
2417                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2418                         /* an Add(a, NULL) is a hidden Conv */
2419                         dbg_info *dbg = get_irn_dbg_info(n);
2420                         return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2421                 }
2422         }
2423
2424         if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2425                 ir_tarval *tv  = get_Const_tarval(b);
2426                 ir_tarval *min = get_mode_min(mode);
2427                 /* if all bits are set, then this has the same effect as a Not.
2428                  * Note that the following == gives false for different modes which
2429                  * is exactly what we want */
2430                 if (tv == min) {
2431                         dbg_info *dbgi  = get_irn_dbg_info(n);
2432                         ir_graph *irg   = get_irn_irg(n);
2433                         ir_node  *block = get_nodes_block(n);
2434                         ir_node  *cnst  = new_r_Const(irg, min);
2435                         return new_rd_Eor(dbgi, block, a, cnst, mode);
2436                 }
2437         }
2438
2439         HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2440
2441         /* for FP the following optimizations are only allowed if
2442          * fp_strict_algebraic is disabled */
2443         if (mode_is_float(mode)) {
2444                 ir_graph *irg = get_irn_irg(n);
2445                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2446                         return n;
2447         }
2448
2449         if (mode_is_num(mode)) {
2450                 ir_graph *irg = get_irn_irg(n);
2451                 /* the following code leads to endless recursion when Mul are replaced
2452                  * by a simple instruction chain */
2453                 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2454                                 && a == b && mode_is_int(mode)) {
2455                         ir_node *block = get_nodes_block(n);
2456
2457                         n = new_rd_Mul(
2458                                 get_irn_dbg_info(n),
2459                                 block,
2460                                 a,
2461                                 new_r_Const_long(irg, mode, 2),
2462                                 mode);
2463                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2464                         return n;
2465                 }
2466                 if (is_Minus(a)) {
2467                         n = new_rd_Sub(
2468                                         get_irn_dbg_info(n),
2469                                         get_nodes_block(n),
2470                                         b,
2471                                         get_Minus_op(a),
2472                                         mode);
2473                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2474                         return n;
2475                 }
2476                 if (is_Minus(b)) {
2477                         n = new_rd_Sub(
2478                                         get_irn_dbg_info(n),
2479                                         get_nodes_block(n),
2480                                         a,
2481                                         get_Minus_op(b),
2482                                         mode);
2483                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2484                         return n;
2485                 }
2486                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2487                         /* Here we rely on constants be on the RIGHT side */
2488                         if (is_Not(a)) {
2489                                 ir_node *op = get_Not_op(a);
2490
2491                                 if (is_Const(b) && is_Const_one(b)) {
2492                                         /* ~x + 1 = -x */
2493                                         ir_node *blk = get_nodes_block(n);
2494                                         n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2495                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2496                                         return n;
2497                                 }
2498                         }
2499                 }
2500         }
2501
2502         if (is_Or_Eor_Add(n)) {
2503                 n = transform_node_Or_(n);
2504                 if (n != oldn)
2505                         return n;
2506                 n = transform_node_Eor_(n);
2507                 if (n != oldn)
2508                         return n;
2509         }
2510
2511         return n;
2512 }
2513
2514 /**
2515  * returns -cnst or NULL if impossible
2516  */
2517 static ir_node *const_negate(ir_node *cnst)
2518 {
2519         ir_tarval *tv    = tarval_neg(get_Const_tarval(cnst));
2520         dbg_info  *dbgi  = get_irn_dbg_info(cnst);
2521         ir_graph  *irg   = get_irn_irg(cnst);
2522         if (tv == tarval_bad) return NULL;
2523         return new_rd_Const(dbgi, irg, tv);
2524 }
2525
2526 /**
2527  * Do the AddSub optimization, then Transform
2528  *   Constant folding on Phi
2529  *   Sub(0,a)          -> Minus(a)
2530  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2531  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2532  *   Sub(Add(a, x), x) -> a
2533  *   Sub(x, Add(x, a)) -> -a
2534  *   Sub(x, Const)     -> Add(x, -Const)
2535  */
2536 static ir_node *transform_node_Sub(ir_node *n)
2537 {
2538         ir_mode *mode;
2539         ir_node *oldn = n;
2540         ir_node *a, *b, *c;
2541
2542         n = transform_node_AddSub(n);
2543
2544         a = get_Sub_left(n);
2545         b = get_Sub_right(n);
2546
2547         mode = get_irn_mode(n);
2548
2549         if (mode_is_int(mode)) {
2550                 ir_mode *lmode = get_irn_mode(a);
2551
2552                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2553                         /* a Sub(a, NULL) is a hidden Conv */
2554                         dbg_info *dbg = get_irn_dbg_info(n);
2555                         n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2556                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2557                         return n;
2558                 }
2559
2560                 if (mode == lmode                                     &&
2561                     get_mode_arithmetic(mode) == irma_twos_complement &&
2562                     is_Const(a)                                       &&
2563                     get_Const_tarval(a) == get_mode_minus_one(mode)) {
2564                         /* -1 - x -> ~x */
2565                         dbg_info *dbg = get_irn_dbg_info(n);
2566                         n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2567                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2568                         return n;
2569                 }
2570         }
2571
2572 restart:
2573         HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2574
2575         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2576         if (mode_is_float(mode)) {
2577                 ir_graph *irg = get_irn_irg(n);
2578                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2579                         return n;
2580         }
2581
2582         if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2583                 /* a - C -> a + (-C) */
2584                 ir_node *cnst = const_negate(b);
2585                 if (cnst != NULL) {
2586                         ir_node  *block = get_nodes_block(n);
2587                         dbg_info *dbgi  = get_irn_dbg_info(n);
2588
2589                         n = new_rd_Add(dbgi, block, a, cnst, mode);
2590                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2591                         return n;
2592                 }
2593         }
2594
2595         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2596                 dbg_info *dbg   = get_irn_dbg_info(n);
2597                 ir_node  *block = get_nodes_block(n);
2598                 ir_node  *left  = get_Minus_op(a);
2599                 ir_node  *add   = new_rd_Add(dbg, block, left, b, mode);
2600
2601                 n = new_rd_Minus(dbg, block, add, mode);
2602                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2603                 return n;
2604         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2605                 dbg_info *dbg   = get_irn_dbg_info(n);
2606                 ir_node  *block = get_nodes_block(n);
2607                 ir_node  *right = get_Minus_op(b);
2608
2609                 n = new_rd_Add(dbg, block, a, right, mode);
2610                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2611                 return n;
2612         } else if (is_Sub(b)) {
2613                 /* a - (b - c) -> a + (c - b)
2614                  *             -> (a - b) + c iff (b - c) is a pointer */
2615                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2616                 ir_node  *s_left  = get_Sub_left(b);
2617                 ir_node  *s_right = get_Sub_right(b);
2618                 ir_mode  *s_mode  = get_irn_mode(b);
2619                 if (mode_is_reference(s_mode)) {
2620                         ir_node  *lowest_block = get_nodes_block(n); /* a and b are live here */
2621                         ir_node  *sub     = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2622                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2623
2624                         if (s_mode != mode)
2625                                 s_right = new_r_Conv(lowest_block, s_right, mode);
2626                         n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2627                 } else {
2628                         ir_node  *s_block = get_nodes_block(b);
2629                         ir_node  *sub     = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2630                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2631                         ir_node  *a_block = get_nodes_block(n);
2632
2633                         n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2634                 }
2635                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2636                 return n;
2637         }
2638
2639         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2640         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2641                 n = new_rd_Minus(
2642                                 get_irn_dbg_info(n),
2643                                 get_nodes_block(n),
2644                                 b,
2645                                 mode);
2646                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2647                 return n;
2648         }
2649         if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2650                 ir_node *left  = get_binop_left(a);
2651                 ir_node *right = get_binop_right(a);
2652
2653                 /* FIXME: Does the Conv's work only for two complement or generally? */
2654                 if (left == b) {
2655                         if (mode != get_irn_mode(right)) {
2656                                 /* This Sub is an effective Cast */
2657                                 right = new_r_Conv(get_nodes_block(n), right, mode);
2658                         }
2659                         n = right;
2660                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2661                         return n;
2662                 } else if (right == b) {
2663                         if (mode != get_irn_mode(left)) {
2664                                 /* This Sub is an effective Cast */
2665                                 left = new_r_Conv(get_nodes_block(n), left, mode);
2666                         }
2667                         n = left;
2668                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2669                         return n;
2670                 }
2671         }
2672         if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2673                 ir_node *left  = get_binop_left(b);
2674                 ir_node *right = get_binop_right(b);
2675
2676                 /* FIXME: Does the Conv's work only for two complement or generally? */
2677                 if (left == a) {
2678                         ir_mode *r_mode = get_irn_mode(right);
2679
2680                         n = new_r_Minus(get_nodes_block(n), right, r_mode);
2681                         if (mode != r_mode) {
2682                                 /* This Sub is an effective Cast */
2683                                 n = new_r_Conv(get_nodes_block(n), n, mode);
2684                         }
2685                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2686                         return n;
2687                 } else if (right == a) {
2688                         ir_mode *l_mode = get_irn_mode(left);
2689
2690                         n = new_r_Minus(get_nodes_block(n), left, l_mode);
2691                         if (mode != l_mode) {
2692                                 /* This Sub is an effective Cast */
2693                                 n = new_r_Conv(get_nodes_block(n), n, mode);
2694                         }
2695                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2696                         return n;
2697                 }
2698         }
2699         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2700                 ir_mode *mode = get_irn_mode(a);
2701
2702                 if (mode == get_irn_mode(b)) {
2703                         ir_mode *ma, *mb;
2704                         ir_node *op_a = get_Conv_op(a);
2705                         ir_node *op_b = get_Conv_op(b);
2706
2707                         /* check if it's allowed to skip the conv */
2708                         ma = get_irn_mode(op_a);
2709                         mb = get_irn_mode(op_b);
2710
2711                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2712                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2713                                 a = op_a; b = op_b;
2714                                 set_Sub_left(n, a);
2715                                 set_Sub_right(n, b);
2716
2717                                 goto restart;
2718                         }
2719                 }
2720         }
2721         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2722         if (!is_reassoc_running() && is_Mul(a)) {
2723                 ir_node *ma = get_Mul_left(a);
2724                 ir_node *mb = get_Mul_right(a);
2725
2726                 if (ma == b) {
2727                         ir_node  *blk = get_nodes_block(n);
2728                         ir_graph *irg = get_irn_irg(n);
2729                         n = new_rd_Mul(
2730                                         get_irn_dbg_info(n),
2731                                         blk,
2732                                         ma,
2733                                         new_rd_Sub(
2734                                                 get_irn_dbg_info(n),
2735                                                 blk,
2736                                                 mb,
2737                                                 new_r_Const(irg, get_mode_one(mode)),
2738                                                 mode),
2739                                         mode);
2740                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2741                         return n;
2742                 } else if (mb == b) {
2743                         ir_node  *blk = get_nodes_block(n);
2744                         ir_graph *irg = get_irn_irg(n);
2745                         n = new_rd_Mul(
2746                                         get_irn_dbg_info(n),
2747                                         blk,
2748                                         mb,
2749                                         new_rd_Sub(
2750                                                 get_irn_dbg_info(n),
2751                                                 blk,
2752                                                 ma,
2753                                                 new_r_Const(irg, get_mode_one(mode)),
2754                                                 mode),
2755                                         mode);
2756                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2757                         return n;
2758                 }
2759         }
2760         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2761                 ir_node *x        = get_Sub_left(a);
2762                 ir_node *y        = get_Sub_right(a);
2763                 ir_node *blk      = get_nodes_block(n);
2764                 ir_mode *m_b      = get_irn_mode(b);
2765                 ir_mode *m_y      = get_irn_mode(y);
2766                 ir_mode *add_mode;
2767                 ir_node *add;
2768
2769                 /* Determine the right mode for the Add. */
2770                 if (m_b == m_y)
2771                         add_mode = m_b;
2772                 else if (mode_is_reference(m_b))
2773                         add_mode = m_b;
2774                 else if (mode_is_reference(m_y))
2775                         add_mode = m_y;
2776                 else {
2777                         /*
2778                          * Both modes are different but none is reference,
2779                          * happens for instance in SubP(SubP(P, Iu), Is).
2780                          * We have two possibilities here: Cast or ignore.
2781                          * Currently we ignore this case.
2782                          */
2783                         return n;
2784                 }
2785
2786                 add = new_r_Add(blk, y, b, add_mode);
2787
2788                 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2789                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2790                 return n;
2791         }
2792
2793         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2794                 /* c - ~X = X + (c+1) */
2795                 if (is_Const(a) && is_Not(b)) {
2796                         ir_tarval *tv = get_Const_tarval(a);
2797
2798                         tv = tarval_add(tv, get_mode_one(mode));
2799                         if (tv != tarval_bad) {
2800                                 ir_node  *blk = get_nodes_block(n);
2801                                 ir_graph *irg = get_irn_irg(n);
2802                                 ir_node *c = new_r_Const(irg, tv);
2803                                 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2804                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2805                                 return n;
2806                         }
2807                 }
2808                 /* x-(x&y) = x & ~y */
2809                 if (is_And(b)) {
2810                         ir_node *and_left  = get_And_left(b);
2811                         ir_node *and_right = get_And_right(b);
2812                         if (and_right == a) {
2813                                 ir_node *tmp = and_left;
2814                                 and_left  = and_right;
2815                                 and_right = tmp;
2816                         }
2817                         if (and_left == a) {
2818                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2819                                 ir_node  *block = get_nodes_block(n);
2820                                 ir_mode  *mode  = get_irn_mode(n);
2821                                 ir_node  *notn  = new_rd_Not(dbgi, block, and_right, mode);
2822                                 ir_node  *andn  = new_rd_And(dbgi, block, a, notn, mode);
2823                                 return andn;
2824                         }
2825                 }
2826         }
2827         return n;
2828 }
2829
2830 /**
2831  * Several transformation done on n*n=2n bits mul.
2832  * These transformations must be done here because new nodes may be produced.
2833  */
2834 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2835 {
2836         ir_node   *oldn  = n;
2837         ir_node   *a     = get_Mul_left(n);
2838         ir_node   *b     = get_Mul_right(n);
2839         ir_tarval *ta    = value_of(a);
2840         ir_tarval *tb    = value_of(b);
2841         ir_mode   *smode = get_irn_mode(a);
2842
2843         if (ta == get_mode_one(smode)) {
2844                 /* (L)1 * (L)b = (L)b */
2845                 ir_node *blk = get_nodes_block(n);
2846                 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2847                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2848                 return n;
2849         }
2850         else if (ta == get_mode_minus_one(smode)) {
2851                 /* (L)-1 * (L)b = (L)b */
2852                 ir_node *blk = get_nodes_block(n);
2853                 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2854                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2855                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2856                 return n;
2857         }
2858         if (tb == get_mode_one(smode)) {
2859                 /* (L)a * (L)1 = (L)a */
2860                 ir_node *blk = get_nodes_block(a);
2861                 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2862                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2863                 return n;
2864         }
2865         else if (tb == get_mode_minus_one(smode)) {
2866                 /* (L)a * (L)-1 = (L)-a */
2867                 ir_node *blk = get_nodes_block(n);
2868                 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2869                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2870                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2871                 return n;
2872         }
2873         return n;
2874 }
2875
2876 /**
2877  * Transform Mul(a,-1) into -a.
2878  * Do constant evaluation of Phi nodes.
2879  * Do architecture dependent optimizations on Mul nodes
2880  */
2881 static ir_node *transform_node_Mul(ir_node *n)
2882 {
2883         ir_node *c, *oldn = n;
2884         ir_mode *mode = get_irn_mode(n);
2885         ir_node *a = get_Mul_left(n);
2886         ir_node *b = get_Mul_right(n);
2887
2888         n = fold_constant_associativity(n, tarval_mul);
2889         if (n != oldn)
2890                 return n;
2891
2892         if (mode != get_irn_mode(a))
2893                 return transform_node_Mul2n(n, mode);
2894
2895         HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2896
2897         if (mode_is_signed(mode)) {
2898                 ir_node *r = NULL;
2899
2900                 if (value_of(a) == get_mode_minus_one(mode))
2901                         r = b;
2902                 else if (value_of(b) == get_mode_minus_one(mode))
2903                         r = a;
2904                 if (r) {
2905                         n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2906                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2907                         return n;
2908                 }
2909         }
2910         if (is_Minus(a)) {
2911                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2912                         ir_node *cnst = const_negate(b);
2913                         if (cnst != NULL) {
2914                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2915                                 ir_node  *block = get_nodes_block(n);
2916                                 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2917                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2918                                 return n;
2919                         }
2920                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2921                         dbg_info *dbgi  = get_irn_dbg_info(n);
2922                         ir_node  *block = get_nodes_block(n);
2923                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2924                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2925                         return n;
2926                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2927                         ir_node  *sub_l = get_Sub_left(b);
2928                         ir_node  *sub_r = get_Sub_right(b);
2929                         dbg_info *dbgi  = get_irn_dbg_info(n);
2930                         ir_node  *block = get_nodes_block(n);
2931                         ir_node  *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2932                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2933                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2934                         return n;
2935                 }
2936         } else if (is_Minus(b)) {
2937                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2938                         ir_node  *sub_l = get_Sub_left(a);
2939                         ir_node  *sub_r = get_Sub_right(a);
2940                         dbg_info *dbgi  = get_irn_dbg_info(n);
2941                         ir_node  *block = get_nodes_block(n);
2942                         ir_node  *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2943                         n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2944                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2945                         return n;
2946                 }
2947         } else if (is_Shl(a)) {
2948                 ir_node *const shl_l = get_Shl_left(a);
2949                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2950                         /* (1 << x) * b -> b << x */
2951                         dbg_info *const dbgi  = get_irn_dbg_info(n);
2952                         ir_node  *const block = get_nodes_block(n);
2953                         ir_node  *const shl_r = get_Shl_right(a);
2954                         n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2955                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2956                         return n;
2957                 }
2958         } else if (is_Shl(b)) {
2959                 ir_node *const shl_l = get_Shl_left(b);
2960                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2961                         /* a * (1 << x) -> a << x */
2962                         dbg_info *const dbgi  = get_irn_dbg_info(n);
2963                         ir_node  *const block = get_nodes_block(n);
2964                         ir_node  *const shl_r = get_Shl_right(b);
2965                         n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2966                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2967                         return n;
2968                 }
2969         }
2970         if (get_mode_arithmetic(mode) == irma_ieee754
2971             || get_mode_arithmetic(mode) == irma_x86_extended_float) {
2972                 if (is_Const(a)) {
2973                         ir_tarval *tv = get_Const_tarval(a);
2974                         if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2975                                         && !tarval_is_negative(tv)) {
2976                                 /* 2.0 * b = b + b */
2977                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2978                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2979                                 return n;
2980                         }
2981                 }
2982                 else if (is_Const(b)) {
2983                         ir_tarval *tv = get_Const_tarval(b);
2984                         if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2985                                         && !tarval_is_negative(tv)) {
2986                                 /* a * 2.0 = a + a */
2987                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2988                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2989                                 return n;
2990                         }
2991                 }
2992         }
2993         return arch_dep_replace_mul_with_shifts(n);
2994 }
2995
2996 /**
2997  * Transform a Div Node.
2998  */
2999 static ir_node *transform_node_Div(ir_node *n)
3000 {
3001         ir_mode *mode = get_Div_resmode(n);
3002         ir_node *a = get_Div_left(n);
3003         ir_node *b = get_Div_right(n);
3004         ir_node *value = n;
3005         const ir_node *dummy;
3006
3007         if (mode_is_int(mode)) {
3008                 if (is_Const(b) && is_const_Phi(a)) {
3009                         /* check for Div(Phi, Const) */
3010                         value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3011                         if (value) {
3012                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3013                                 goto make_tuple;
3014                         }
3015                 } else if (is_Const(a) && is_const_Phi(b)) {
3016                         /* check for Div(Const, Phi) */
3017                         value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3018                         if (value) {
3019                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3020                                 goto make_tuple;
3021                         }
3022                 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3023                         /* check for Div(Phi, Phi) */
3024                         value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3025                         if (value) {
3026                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3027                                 goto make_tuple;
3028                         }
3029                 }
3030
3031                 if (a == b && value_not_zero(a, &dummy)) {
3032                         ir_graph *irg = get_irn_irg(n);
3033                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3034                         value = new_r_Const(irg, get_mode_one(mode));
3035                         DBG_OPT_CSTEVAL(n, value);
3036                         goto make_tuple;
3037                 } else {
3038                         if (mode_is_signed(mode) && is_Const(b)) {
3039                                 ir_tarval *tv = get_Const_tarval(b);
3040
3041                                 if (tv == get_mode_minus_one(mode)) {
3042                                         /* a / -1 */
3043                                         value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3044                                         DBG_OPT_CSTEVAL(n, value);
3045                                         goto make_tuple;
3046                                 }
3047                         }
3048                         /* Try architecture dependent optimization */
3049                         value = arch_dep_replace_div_by_const(n);
3050                 }
3051         } else {
3052                 assert(mode_is_float(mode));
3053
3054                 /* Optimize x/c to x*(1/c) */
3055                 ir_tarval *tv = value_of(b);
3056
3057                 if (tv != tarval_bad) {
3058                         tv = tarval_div(get_mode_one(mode), tv);
3059
3060                         /* Do the transformation if the result is either exact or we are
3061                            not using strict rules. */
3062                         if (tv != tarval_bad &&
3063                                 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3064                                 ir_node  *block = get_nodes_block(n);
3065                                 ir_graph *irg   = get_irn_irg(block);
3066                                 ir_node  *c     = new_r_Const(irg, tv);
3067                                 dbg_info *dbgi  = get_irn_dbg_info(n);
3068                                 value = new_rd_Mul(dbgi, block, a, c, mode);
3069
3070                                 goto make_tuple;
3071                         }
3072                 }
3073         }
3074
3075         if (value != n) {
3076                 ir_node *mem, *blk;
3077                 ir_graph *irg;
3078
3079 make_tuple:
3080                 /* Turn Div into a tuple (mem, jmp, bad, value) */
3081                 mem = get_Div_mem(n);
3082                 blk = get_nodes_block(n);
3083                 irg = get_irn_irg(blk);
3084
3085                 /* skip a potential Pin */
3086                 mem = skip_Pin(mem);
3087                 ir_node *const in[] = {
3088                         [pn_Div_M]         = mem,
3089                         [pn_Div_res]       = value,
3090                         [pn_Div_X_regular] = new_r_Jmp(blk),
3091                         [pn_Div_X_except]  = new_r_Bad(irg, mode_X),
3092                 };
3093                 turn_into_tuple(n, ARRAY_SIZE(in), in);
3094         }
3095         return n;
3096 }
3097
3098 /**
3099  * Transform a Mod node.
3100  */
3101 static ir_node *transform_node_Mod(ir_node *n)
3102 {
3103         ir_mode   *mode = get_Mod_resmode(n);
3104         ir_node   *a    = get_Mod_left(n);
3105         ir_node   *b    = get_Mod_right(n);
3106         ir_graph  *irg;
3107         ir_node   *value;
3108         ir_tarval *tv;
3109
3110         if (is_Const(b) && is_const_Phi(a)) {
3111                 /* check for Div(Phi, Const) */
3112                 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3113                 if (value) {
3114                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3115                         goto make_tuple;
3116                 }
3117         }
3118         else if (is_Const(a) && is_const_Phi(b)) {
3119                 /* check for Div(Const, Phi) */
3120                 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3121                 if (value) {
3122                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3123                         goto make_tuple;
3124                 }
3125         }
3126         else if (is_const_Phi(a) && is_const_Phi(b)) {
3127                 /* check for Div(Phi, Phi) */
3128                 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3129                 if (value) {
3130                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3131                         goto make_tuple;
3132                 }
3133         }
3134
3135         value = n;
3136         tv = value_of(n);
3137         irg = get_irn_irg(n);
3138         if (tv != tarval_bad) {
3139                 value = new_r_Const(irg, tv);
3140
3141                 DBG_OPT_CSTEVAL(n, value);
3142                 goto make_tuple;
3143         } else {
3144                 ir_node       *a = get_Mod_left(n);
3145                 ir_node       *b = get_Mod_right(n);
3146                 const ir_node *dummy;
3147
3148                 if (a == b && value_not_zero(a, &dummy)) {
3149                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3150                         value = new_r_Const(irg, get_mode_null(mode));
3151                         DBG_OPT_CSTEVAL(n, value);
3152                         goto make_tuple;
3153                 } else {
3154                         if (mode_is_signed(mode) && is_Const(b)) {
3155                                 ir_tarval *tv = get_Const_tarval(b);
3156
3157                                 if (tv == get_mode_minus_one(mode)) {
3158                                         /* a % -1 = 0 */
3159                                         value = new_r_Const(irg, get_mode_null(mode));
3160                                         DBG_OPT_CSTEVAL(n, value);
3161                                         goto make_tuple;
3162                                 }
3163                         }
3164                         /* Try architecture dependent optimization */
3165                         value = arch_dep_replace_mod_by_const(n);
3166                 }
3167         }
3168
3169         if (value != n) {
3170                 ir_node *mem, *blk;
3171                 ir_graph *irg;
3172
3173 make_tuple:
3174                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3175                 mem = get_Mod_mem(n);
3176                 blk = get_nodes_block(n);
3177                 irg = get_irn_irg(blk);
3178
3179                 /* skip a potential Pin */
3180                 mem = skip_Pin(mem);
3181                 ir_node *const in[] = {
3182                         [pn_Mod_M]         = mem,
3183                         [pn_Mod_res]       = value,
3184                         [pn_Mod_X_regular] = new_r_Jmp(blk),
3185                         [pn_Mod_X_except]  = new_r_Bad(irg, mode_X),
3186                 };
3187                 turn_into_tuple(n, ARRAY_SIZE(in), in);
3188         }
3189         return n;
3190 }
3191
3192 /**
3193  * Transform a Cond node.
3194  *
3195  * Replace the Cond by a Jmp if it branches on a constant
3196  * condition.
3197  */
3198 static ir_node *transform_node_Cond(ir_node *n)
3199 {
3200         ir_node   *a   = get_Cond_selector(n);
3201         ir_graph  *irg = get_irn_irg(n);
3202         ir_tarval *ta;
3203
3204         /* we need block info which is not available in floating irgs */
3205         if (get_irg_pinned(irg) == op_pin_state_floats)
3206                 return n;
3207
3208         ta = value_of(a);
3209         if (ta == tarval_bad && is_Cmp(a)) {
3210                 /* try again with a direct call to compute_cmp, as we don't care
3211                  * about the MODEB_LOWERED flag here */
3212                 ta = compute_cmp_ext(a);
3213         }
3214
3215         if (ta != tarval_bad) {
3216                 /* It's branching on a boolean constant.
3217                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3218                 ir_node *const blk  = get_nodes_block(n);
3219                 ir_node *const jmp  = new_r_Jmp(blk);
3220                 ir_node *const bad  = new_r_Bad(irg, mode_X);
3221                 bool     const cond = ta == tarval_b_true;
3222                 ir_node *const in[] = {
3223                         [pn_Cond_false] = cond ? bad : jmp,
3224                         [pn_Cond_true]  = cond ? jmp : bad,
3225                 };
3226                 turn_into_tuple(n, ARRAY_SIZE(in), in);
3227                 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3228         }
3229         return n;
3230 }
3231
3232 static ir_node *transform_node_Switch(ir_node *n)
3233 {
3234         ir_node   *op  = get_Switch_selector(n);
3235         ir_tarval *val = value_of(op);
3236         if (val != tarval_bad) {
3237                 dbg_info              *dbgi      = get_irn_dbg_info(n);
3238                 ir_graph              *irg       = get_irn_irg(n);
3239                 unsigned               n_outs    = get_Switch_n_outs(n);
3240                 ir_node               *block     = get_nodes_block(n);
3241                 ir_node               *bad       = new_r_Bad(irg, mode_X);
3242                 ir_node              **in        = XMALLOCN(ir_node*, n_outs);
3243                 const ir_switch_table *table     = get_Switch_table(n);
3244                 size_t                 n_entries = ir_switch_table_get_n_entries(table);
3245                 long                   jmp_pn    = 0;
3246                 size_t                 i;
3247                 unsigned               o;
3248                 for (i = 0; i < n_entries; ++i) {
3249                         const ir_switch_table_entry *entry
3250                                 = ir_switch_table_get_entry_const(table, i);
3251                         ir_tarval *min = entry->min;
3252                         ir_tarval *max = entry->max;
3253                         if (entry->pn == 0)
3254                                 continue;
3255                         if ((min == max && min == val)
3256                             || (tarval_cmp(val, min) != ir_relation_less
3257                                 && tarval_cmp(val, max) != ir_relation_greater)) {
3258                             jmp_pn = entry->pn;
3259                             break;
3260                         }
3261                 }
3262                 for (o = 0; o < n_outs; ++o) {
3263                         if (o == (unsigned)jmp_pn) {
3264                                 in[o] = new_rd_Jmp(dbgi, block);
3265                         } else {
3266                                 in[o] = bad;
3267                         }
3268                 }
3269                 return new_r_Tuple(block, (int)n_outs, in);
3270         }
3271         return n;
3272 }
3273
3274 /**
3275  * normalisation: (x & c1) >> c2   to   (x >> c2) & (c1 >> c2)
3276  *  (we can use:
3277  *    - and, or, xor          instead of &
3278  *    - Shl, Shr, Shrs, rotl  instead of >>
3279  *    (with a special case for Or/Xor + Shrs)
3280  *
3281  * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3282  */
3283 static ir_node *transform_node_shift_bitop(ir_node *n)
3284 {
3285         ir_graph  *irg   = get_irn_irg(n);
3286         ir_node   *right = get_binop_right(n);
3287         ir_mode   *mode  = get_irn_mode(n);
3288         ir_node   *left;
3289         ir_node   *bitop_left;
3290         ir_node   *bitop_right;
3291         ir_op     *op_left;
3292         ir_node   *block;
3293         dbg_info  *dbgi;
3294         ir_node   *new_shift;
3295         ir_node   *new_bitop;
3296         ir_node   *new_const;
3297         ir_tarval *tv1;
3298         ir_tarval *tv2;
3299         ir_tarval *tv_shift;
3300
3301         if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3302                 return n;
3303
3304         assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3305
3306         if (!is_Const(right))
3307                 return n;
3308
3309         left    = get_binop_left(n);
3310         op_left = get_irn_op(left);
3311         if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3312                 return n;
3313
3314         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3315         if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3316                 /* TODO: test if sign bit is affectes */
3317                 return n;
3318         }
3319
3320         bitop_right = get_binop_right(left);
3321         if (!is_Const(bitop_right))
3322                 return n;
3323
3324         bitop_left = get_binop_left(left);
3325
3326         block = get_nodes_block(n);
3327         dbgi  = get_irn_dbg_info(n);
3328         tv1   = get_Const_tarval(bitop_right);
3329         tv2   = get_Const_tarval(right);
3330
3331         assert(get_tarval_mode(tv1) == mode);
3332
3333         if (is_Shl(n)) {
3334                 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3335                 tv_shift  = tarval_shl(tv1, tv2);
3336         } else if (is_Shr(n)) {
3337                 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3338                 tv_shift  = tarval_shr(tv1, tv2);
3339         } else if (is_Shrs(n)) {
3340                 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3341                 tv_shift  = tarval_shrs(tv1, tv2);
3342         } else {
3343                 assert(is_Rotl(n));
3344                 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3345                 tv_shift  = tarval_rotl(tv1, tv2);
3346         }
3347
3348         assert(get_tarval_mode(tv_shift) == mode);
3349         irg       = get_irn_irg(n);
3350         new_const = new_r_Const(irg, tv_shift);
3351
3352         if (op_left == op_And) {
3353                 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3354         } else if (op_left == op_Or) {
3355                 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3356         } else {
3357                 assert(op_left == op_Eor);
3358                 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3359         }
3360
3361         return new_bitop;
3362 }
3363
3364 /**
3365  * Transform an And.
3366  */
3367 static ir_node *transform_node_And(ir_node *n)
3368 {
3369         ir_node *c, *oldn = n;
3370         ir_node *a = get_And_left(n);
3371         ir_node *b = get_And_right(n);
3372         ir_mode *mode;
3373
3374         n = fold_constant_associativity(n, tarval_and);
3375         if (n != oldn)
3376                 return n;
3377
3378         if (is_Cmp(a) && is_Cmp(b)) {
3379                 ir_node    *a_left     = get_Cmp_left(a);
3380                 ir_node    *a_right    = get_Cmp_right(a);
3381                 ir_node    *b_left     = get_Cmp_left(b);
3382                 ir_node    *b_right    = get_Cmp_right(b);
3383                 ir_relation a_relation = get_Cmp_relation(a);
3384                 ir_relation b_relation = get_Cmp_relation(b);
3385                 /* we can combine the relations of two compares with the same
3386                  * operands */
3387                 if (a_left == b_left && b_left == b_right) {
3388                         dbg_info   *dbgi         = get_irn_dbg_info(n);
3389                         ir_node    *block        = get_nodes_block(n);
3390                         ir_relation new_relation = a_relation & b_relation;
3391                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3392                 }
3393                 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3394                 if (a_relation == b_relation && a_relation == ir_relation_equal
3395                     && !mode_is_float(get_irn_mode(a_left))
3396                     && !mode_is_float(get_irn_mode(b_left))) {
3397                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3398                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3399                                 ir_node  *block  = get_nodes_block(n);
3400                                 ir_mode  *a_mode = get_irn_mode(a_left);
3401                                 ir_mode  *b_mode = get_irn_mode(b_left);
3402                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3403                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3404                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
3405                                 ir_node  *orn    = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3406                                 ir_graph *irg    = get_irn_irg(n);
3407                                 ir_node  *zero   = create_zero_const(irg, b_mode);
3408                                 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3409                         }
3410                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3411                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3412                                 ir_node  *block  = get_nodes_block(n);
3413                                 ir_mode  *a_mode = get_irn_mode(a_left);
3414                                 ir_mode  *b_mode = get_irn_mode(b_left);
3415                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3416                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3417                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
3418                                 ir_node  *orn    = new_rd_Or(dbgi, block, xora, conv, a_mode);
3419                                 ir_graph *irg    = get_irn_irg(n);
3420                                 ir_node  *zero   = create_zero_const(irg, a_mode);
3421                                 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3422                         }
3423                 }
3424         }
3425
3426         mode = get_irn_mode(n);
3427         HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3428
3429         if (is_Or(a) || is_Or_Eor_Add(a)) {
3430                 ir_node *or_left  = get_binop_left(a);
3431                 ir_node *or_right = get_binop_right(a);
3432                 if (complement_values(or_left, b)) {
3433                         /* (a|b) & ~a => b & ~a */
3434                         dbg_info *dbgi    = get_irn_dbg_info(n);
3435                         ir_node  *block   = get_nodes_block(n);
3436                         return new_rd_And(dbgi, block, or_right, b, mode);
3437                 } else if (complement_values(or_right, b)) {
3438                         /* (a|b) & ~b => a & ~b */
3439                         dbg_info *dbgi    = get_irn_dbg_info(n);
3440                         ir_node  *block   = get_nodes_block(n);
3441                         return new_rd_And(dbgi, block, or_left, b, mode);
3442                 } else if (is_Not(b)) {
3443                         ir_node *op = get_Not_op(b);
3444                         if (is_And(op)) {
3445                                 ir_node *ba = get_And_left(op);
3446                                 ir_node *bb = get_And_right(op);
3447
3448                                 /* it's enough to test the following cases due to normalization! */
3449                                 if (or_left == ba && or_right == bb) {
3450                                         /* (a|b) & ~(a&b) = a^b */
3451                                         ir_node *block = get_nodes_block(n);
3452
3453                                         n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3454                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3455                                         return n;
3456                                 }
3457                         }
3458                 }
3459         }
3460         if (is_Or(b) || is_Or_Eor_Add(b)) {
3461                 ir_node *or_left  = get_binop_left(b);
3462                 ir_node *or_right = get_binop_right(b);
3463                 if (complement_values(or_left, a)) {
3464                         /* (a|b) & ~a => b & ~a */
3465                         dbg_info *dbgi    = get_irn_dbg_info(n);
3466                         ir_node  *block   = get_nodes_block(n);
3467                         return new_rd_And(dbgi, block, or_right, a, mode);
3468                 } else if (complement_values(or_right, a)) {
3469                         /* (a|b) & ~b => a & ~b */
3470                         dbg_info *dbgi    = get_irn_dbg_info(n);
3471                         ir_node  *block   = get_nodes_block(n);
3472                         return new_rd_And(dbgi, block, or_left, a, mode);
3473                 } else if (is_Not(a)) {
3474                         ir_node *op = get_Not_op(a);
3475                         if (is_And(op)) {
3476                                 ir_node *aa = get_And_left(op);
3477                                 ir_node *ab = get_And_right(op);
3478
3479                                 /* it's enough to test the following cases due to normalization! */
3480                                 if (or_left == aa && or_right == ab) {
3481                                         /* (a|b) & ~(a&b) = a^b */
3482                                         ir_node *block = get_nodes_block(n);
3483
3484                                         n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3485                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3486                                         return n;
3487                                 }
3488                         }
3489                 }
3490         }
3491         if (is_Eor(a) || is_Or_Eor_Add(a)) {
3492                 ir_node *al = get_binop_left(a);
3493                 ir_node *ar = get_binop_right(a);
3494
3495                 if (al == b) {
3496                         /* (b ^ a) & b -> ~a & b */
3497                         dbg_info *dbg  = get_irn_dbg_info(n);
3498                         ir_node *block = get_nodes_block(n);
3499
3500                         ar = new_rd_Not(dbg, block, ar, mode);
3501                         n  = new_rd_And(dbg, block, ar, b, mode);
3502                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3503                         return n;
3504                 }
3505                 if (ar == b) {
3506                         /* (a ^ b) & b -> ~a & b */
3507                         dbg_info *dbg  = get_irn_dbg_info(n);
3508                         ir_node *block = get_nodes_block(n);
3509
3510                         al = new_rd_Not(dbg, block, al, mode);
3511                         n  = new_rd_And(dbg, block, al, b, mode);
3512                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3513                         return n;
3514                 }
3515         }
3516         if (is_Eor(b) || is_Or_Eor_Add(b)) {
3517                 ir_node *bl = get_binop_left(b);
3518                 ir_node *br = get_binop_right(b);
3519
3520                 if (bl == a) {
3521                         /* a & (a ^ b) -> a & ~b */
3522                         dbg_info *dbg  = get_irn_dbg_info(n);
3523                         ir_node *block = get_nodes_block(n);
3524
3525                         br = new_rd_Not(dbg, block, br, mode);
3526                         n  = new_rd_And(dbg, block, br, a, mode);
3527                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3528                         return n;
3529                 }
3530                 if (br == a) {
3531                         /* a & (b ^ a) -> a & ~b */
3532                         dbg_info *dbg  = get_irn_dbg_info(n);
3533                         ir_node *block = get_nodes_block(n);
3534
3535                         bl = new_rd_Not(dbg, block, bl, mode);
3536                         n  = new_rd_And(dbg, block, bl, a, mode);
3537                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3538                         return n;
3539                 }
3540         }
3541         if (is_Not(a) && is_Not(b)) {
3542                 /* ~a & ~b = ~(a|b) */
3543                 ir_node *block = get_nodes_block(n);
3544                 ir_mode *mode = get_irn_mode(n);
3545
3546                 a = get_Not_op(a);
3547                 b = get_Not_op(b);
3548                 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3549                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3550                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3551                 return n;
3552         }
3553
3554         if (is_Const(a)) {
3555                 vrp_attr  *b_vrp = vrp_get_info(b);
3556                 ir_tarval *a_val = get_Const_tarval(a);
3557                 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3558                         return b;
3559                 }
3560         }
3561
3562         if (is_Const(b)) {
3563                 vrp_attr  *a_vrp = vrp_get_info(a);
3564                 ir_tarval *b_val = get_Const_tarval(b);
3565                 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3566                         return a;
3567                 }
3568         }
3569
3570         n = transform_bitwise_distributive(n, transform_node_And);
3571         if (is_And(n))
3572                 n = transform_node_bitop_shift(n);
3573
3574         return n;
3575 }
3576
3577 /**
3578  * Transform a Not.
3579  */
3580 static ir_node *transform_node_Not(ir_node *n)
3581 {
3582         ir_node *c, *oldn = n;
3583         ir_node *a    = get_Not_op(n);
3584         ir_mode *mode = get_irn_mode(n);
3585
3586         HANDLE_UNOP_PHI(tarval_not,a,c);
3587
3588         /* check for a boolean Not */
3589         if (is_Cmp(a)) {
3590                 dbg_info *dbgi  = get_irn_dbg_info(a);
3591                 ir_node  *block = get_nodes_block(a);
3592                 ir_relation relation = get_Cmp_relation(a);
3593                 relation = get_negated_relation(relation);
3594                 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3595                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3596                 return n;
3597         }
3598
3599         /* normalize ~(a ^ b) => a ^ ~b */
3600         if (is_Eor(a) || is_Or_Eor_Add(a)) {
3601                 dbg_info *dbg       = get_irn_dbg_info(n);
3602                 ir_node  *block     = get_nodes_block(n);
3603                 ir_node  *eor_right = get_binop_right(a);
3604                 ir_node  *eor_left  = get_binop_left(a);
3605                 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3606                 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3607                 return n;
3608         }
3609
3610         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3611                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3612                         dbg_info *dbg   = get_irn_dbg_info(n);
3613                         ir_graph *irg   = get_irn_irg(n);
3614                         ir_node  *block = get_nodes_block(n);
3615                         ir_node  *add_l = get_Minus_op(a);
3616                         ir_node  *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3617                         n = new_rd_Add(dbg, block, add_l, add_r, mode);
3618                 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3619                         ir_node *add_r = get_binop_right(a);
3620                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3621                                 /* ~(x + -1) = -x */
3622                                 ir_node *op  = get_binop_left(a);
3623                                 ir_node *blk = get_nodes_block(n);
3624                                 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3625                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3626                         }
3627                 }
3628         }
3629         return n;
3630 }
3631
3632 /**
3633  * Transform a Minus.
3634  * Optimize:
3635  *   -(~x) = x + 1
3636  *   -(a-b) = b - a
3637  *   -(a >>u (size-1)) = a >>s (size-1)
3638  *   -(a >>s (size-1)) = a >>u (size-1)
3639  *   -(a * const) -> a * -const
3640  */
3641 static ir_node *transform_node_Minus(ir_node *n)
3642 {
3643         ir_node *c, *oldn = n;
3644         ir_node *a = get_Minus_op(n);
3645         ir_mode *mode;
3646
3647         HANDLE_UNOP_PHI(tarval_neg,a,c);
3648
3649         mode = get_irn_mode(a);
3650         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3651                 /* the following rules are only to twos-complement */
3652                 if (is_Not(a)) {
3653                         /* -(~x) = x + 1 */
3654                         ir_node   *op  = get_Not_op(a);
3655                         ir_tarval *tv  = get_mode_one(mode);
3656                         ir_node   *blk = get_nodes_block(n);
3657                         ir_graph  *irg = get_irn_irg(blk);
3658                         ir_node   *c   = new_r_Const(irg, tv);
3659                         n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3660                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3661                         return n;
3662                 }
3663                 if (is_Shr(a)) {
3664                         ir_node *c = get_Shr_right(a);
3665
3666                         if (is_Const(c)) {
3667                                 ir_tarval *tv = get_Const_tarval(c);
3668
3669                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3670                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3671                                         ir_node *v = get_Shr_left(a);
3672
3673                                         n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3674                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3675                                         return n;
3676                                 }
3677                         }
3678                 }
3679                 if (is_Shrs(a)) {
3680                         ir_node *c = get_Shrs_right(a);
3681
3682                         if (is_Const(c)) {
3683                                 ir_tarval *tv = get_Const_tarval(c);
3684
3685                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3686                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3687                                         ir_node *v = get_Shrs_left(a);
3688
3689                                         n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3690                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3691                                         return n;
3692                                 }
3693                         }
3694                 }
3695         }
3696         if (is_Sub(a)) {
3697                 /* - (a-b) = b - a */
3698                 ir_node *la  = get_Sub_left(a);
3699                 ir_node *ra  = get_Sub_right(a);
3700                 ir_node *blk = get_nodes_block(n);
3701
3702                 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3703                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3704                 return n;
3705         }
3706
3707         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3708                 ir_node   *mul_l = get_Mul_left(a);
3709                 ir_node   *mul_r = get_Mul_right(a);
3710                 ir_tarval *tv    = value_of(mul_r);
3711                 if (tv != tarval_bad) {
3712                         tv = tarval_neg(tv);
3713                         if (tv != tarval_bad) {
3714                                 ir_graph *irg   = get_irn_irg(n);
3715                                 ir_node  *cnst  = new_r_Const(irg, tv);
3716                                 dbg_info *dbg   = get_irn_dbg_info(a);
3717                                 ir_node  *block = get_nodes_block(a);
3718                                 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3719                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3720                                 return n;
3721                         }
3722                 }
3723         }
3724
3725         return n;
3726 }
3727
3728 /**
3729  * Transform a Proj(Load) with a non-null address.
3730  */
3731 static ir_node *transform_node_Proj_Load(ir_node *proj)
3732 {
3733         if (get_irn_mode(proj) == mode_X) {
3734                 ir_node *load = get_Proj_pred(proj);
3735
3736                 /* get the Load address */
3737                 const ir_node *addr = get_Load_ptr(load);
3738                 const ir_node *confirm;
3739
3740                 if (value_not_null(addr, &confirm)) {
3741                         if (confirm == NULL) {
3742                                 /* this node may float if it did not depend on a Confirm */
3743                                 set_irn_pinned(load, op_pin_state_floats);
3744                         }
3745                         if (get_Proj_proj(proj) == pn_Load_X_except) {
3746                                 ir_graph *irg = get_irn_irg(proj);
3747                                 DBG_OPT_EXC_REM(proj);
3748                                 return new_r_Bad(irg, mode_X);
3749                         } else {
3750                                 ir_node *blk = get_nodes_block(load);
3751                                 return new_r_Jmp(blk);
3752                         }
3753                 }
3754         }
3755         return proj;
3756 }
3757
3758 /**
3759  * Transform a Proj(Store) with a non-null address.
3760  */
3761 static ir_node *transform_node_Proj_Store(ir_node *proj)
3762 {
3763         if (get_irn_mode(proj) == mode_X) {
3764                 ir_node *store = get_Proj_pred(proj);
3765
3766                 /* get the load/store address */
3767                 const ir_node *addr = get_Store_ptr(store);
3768                 const ir_node *confirm;
3769
3770                 if (value_not_null(addr, &confirm)) {
3771                         if (confirm == NULL) {
3772                                 /* this node may float if it did not depend on a Confirm */
3773                                 set_irn_pinned(store, op_pin_state_floats);
3774                         }
3775                         if (get_Proj_proj(proj) == pn_Store_X_except) {
3776                                 ir_graph *irg = get_irn_irg(proj);
3777                                 DBG_OPT_EXC_REM(proj);
3778                                 return new_r_Bad(irg, mode_X);
3779                         } else {
3780                                 ir_node *blk = get_nodes_block(store);
3781                                 return new_r_Jmp(blk);
3782                         }
3783                 }
3784         }
3785         return proj;
3786 }
3787
3788 /**
3789  * Transform a Proj(Div) with a non-zero value.
3790  * Removes the exceptions and routes the memory to the NoMem node.
3791  */
3792 static ir_node *transform_node_Proj_Div(ir_node *proj)
3793 {
3794         ir_node *div = get_Proj_pred(proj);
3795         ir_node *b   = get_Div_right(div);
3796         ir_node *res, *new_mem;
3797         const ir_node *confirm;
3798         long proj_nr;
3799
3800         if (value_not_zero(b, &confirm)) {
3801                 /* div(x, y) && y != 0 */
3802                 if (confirm == NULL) {
3803                         /* we are sure we have a Const != 0 */
3804                         new_mem = get_Div_mem(div);
3805                         new_mem = skip_Pin(new_mem);
3806                         set_Div_mem(div, new_mem);
3807                         set_irn_pinned(div, op_pin_state_floats);
3808                 }
3809
3810                 proj_nr = get_Proj_proj(proj);
3811                 switch (proj_nr) {
3812                 case pn_Div_X_regular:
3813                         return new_r_Jmp(get_nodes_block(div));
3814
3815                 case pn_Div_X_except: {
3816                         ir_graph *irg = get_irn_irg(proj);
3817                         /* we found an exception handler, remove it */
3818                         DBG_OPT_EXC_REM(proj);
3819                         return new_r_Bad(irg, mode_X);
3820                 }
3821
3822                 case pn_Div_M: {
3823                         ir_graph *irg = get_irn_irg(proj);
3824                         res = get_Div_mem(div);
3825                         new_mem = get_irg_no_mem(irg);
3826
3827                         if (confirm) {
3828                                 /* This node can only float up to the Confirm block */
3829                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3830                         }
3831                         set_irn_pinned(div, op_pin_state_floats);
3832                         /* this is a Div without exception, we can remove the memory edge */
3833                         set_Div_mem(div, new_mem);
3834                         return res;
3835                 }
3836                 }
3837         }
3838         return proj;
3839 }
3840
3841 /**
3842  * Transform a Proj(Mod) with a non-zero value.
3843  * Removes the exceptions and routes the memory to the NoMem node.
3844  */
3845 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3846 {
3847         ir_node *mod = get_Proj_pred(proj);
3848         ir_node *b   = get_Mod_right(mod);
3849         ir_node *res, *new_mem;
3850         const ir_node *confirm;
3851         long proj_nr;
3852
3853         if (value_not_zero(b, &confirm)) {
3854                 /* mod(x, y) && y != 0 */
3855                 proj_nr = get_Proj_proj(proj);
3856
3857                 if (confirm == NULL) {
3858                         /* we are sure we have a Const != 0 */
3859                         new_mem = get_Mod_mem(mod);
3860                         new_mem = skip_Pin(new_mem);
3861                         set_Mod_mem(mod, new_mem);
3862                         set_irn_pinned(mod, op_pin_state_floats);
3863                 }
3864
3865                 switch (proj_nr) {
3866
3867                 case pn_Mod_X_regular:
3868                         return new_r_Jmp(get_nodes_block(mod));
3869
3870                 case pn_Mod_X_except: {
3871                         ir_graph *irg = get_irn_irg(proj);
3872                         /* we found an exception handler, remove it */
3873                         DBG_OPT_EXC_REM(proj);
3874                         return new_r_Bad(irg, mode_X);
3875                 }
3876
3877                 case pn_Mod_M: {
3878                         ir_graph *irg = get_irn_irg(proj);
3879                         res = get_Mod_mem(mod);
3880                         new_mem = get_irg_no_mem(irg);
3881
3882                         if (confirm) {
3883                                 /* This node can only float up to the Confirm block */
3884                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3885                         }
3886                         /* this is a Mod without exception, we can remove the memory edge */
3887                         set_Mod_mem(mod, new_mem);
3888                         return res;
3889                 }
3890                 case pn_Mod_res:
3891                         if (get_Mod_left(mod) == b) {
3892                                 /* a % a = 0 if a != 0 */
3893                                 ir_graph *irg  = get_irn_irg(proj);
3894                                 ir_mode  *mode = get_irn_mode(proj);
3895                                 ir_node  *res  = new_r_Const(irg, get_mode_null(mode));
3896
3897                                 DBG_OPT_CSTEVAL(mod, res);
3898                                 return res;
3899                         }
3900                 }
3901         }
3902         return proj;
3903 }
3904
3905 /**
3906  * return true if the operation returns a value with exactly 1 bit set
3907  */
3908 static bool is_single_bit(const ir_node *node)
3909 {
3910         /* a first implementation, could be extended with vrp and others... */
3911         if (is_Shl(node)) {
3912                 ir_node *shl_l  = get_Shl_left(node);
3913                 ir_mode *mode   = get_irn_mode(node);
3914                 int      modulo = get_mode_modulo_shift(mode);
3915                 /* this works if we shift a 1 and we have modulo shift */
3916                 if (is_Const(shl_l) && is_Const_one(shl_l)
3917                                 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3918                         return true;
3919                 }
3920         } else if (is_Const(node)) {
3921                 ir_tarval *tv = get_Const_tarval(node);
3922                 return tarval_is_single_bit(tv);
3923         }
3924         return false;
3925 }
3926
3927 /**
3928  * checks if node just flips a bit in another node and returns that other node
3929  * if so. @p tv should be a value having just 1 bit set
3930  */
3931 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
3932 {
3933         if (is_Not(node))
3934                 return get_Not_op(node);
3935         if (is_Eor(node)) {
3936                 ir_node *right = get_Eor_right(node);
3937                 if (is_Const(right)) {
3938                         ir_tarval *right_tv = get_Const_tarval(right);
3939                         ir_mode   *mode     = get_irn_mode(node);
3940                         if (tarval_and(right_tv, tv) != get_mode_null(mode))
3941                                 return get_Eor_left(node);
3942                 }
3943         }
3944         return NULL;
3945 }
3946
3947 /**
3948  * Normalizes and optimizes Cmp nodes.
3949  */
3950 static ir_node *transform_node_Cmp(ir_node *n)
3951 {
3952         ir_node    *left     = get_Cmp_left(n);
3953         ir_node    *right    = get_Cmp_right(n);
3954         ir_mode    *mode     = get_irn_mode(left);
3955         ir_tarval  *tv       = NULL;
3956         bool        changed  = false;
3957         bool        changedc = false;
3958         ir_relation relation = get_Cmp_relation(n);
3959         ir_relation possible = ir_get_possible_cmp_relations(left, right);
3960
3961         /* mask out impossible relations */
3962         ir_relation new_relation = relation & possible;
3963         if (new_relation != relation) {
3964                 relation = new_relation;
3965                 changed  = true;
3966         }
3967
3968         /* Remove unnecessary conversions */
3969         if (!mode_is_float(mode)
3970             || be_get_backend_param()->mode_float_arithmetic == NULL) {
3971                 if (is_Conv(left) && is_Conv(right)) {
3972                         ir_node *op_left    = get_Conv_op(left);
3973                         ir_node *op_right   = get_Conv_op(right);
3974                         ir_mode *mode_left  = get_irn_mode(op_left);
3975                         ir_mode *mode_right = get_irn_mode(op_right);
3976
3977                         if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3978                                         && mode_left != mode_b && mode_right != mode_b) {
3979                                 ir_node *block = get_nodes_block(n);
3980
3981                                 if (mode_left == mode_right) {
3982                                         left    = op_left;
3983                                         right   = op_right;
3984                                         changed = true;
3985                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3986                                 } else if (smaller_mode(mode_left, mode_right)) {
3987                                         left    = new_r_Conv(block, op_left, mode_right);
3988                                         right   = op_right;
3989                                         changed = true;
3990                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3991                                 } else if (smaller_mode(mode_right, mode_left)) {
3992                                         left    = op_left;
3993                                         right   = new_r_Conv(block, op_right, mode_left);
3994                                         changed = true;
3995                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3996                                 }
3997                                 mode = get_irn_mode(left);
3998                         }
3999                 }
4000                 if (is_Conv(left) && is_Const(right)) {
4001                         ir_node   *op_left   = get_Conv_op(left);
4002                         ir_mode   *mode_left = get_irn_mode(op_left);
4003                         if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4004                                 ir_tarval *tv        = get_Const_tarval(right);
4005                                 tarval_int_overflow_mode_t last_mode
4006                                         = tarval_get_integer_overflow_mode();
4007                                 ir_tarval *new_tv;
4008                                 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4009                                 new_tv = tarval_convert_to(tv, mode_left);
4010                                 tarval_set_integer_overflow_mode(last_mode);
4011                                 if (new_tv != tarval_bad) {
4012                                         ir_graph *irg = get_irn_irg(n);
4013                                         left    = op_left;
4014                                         right   = new_r_Const(irg, new_tv);
4015                                         mode    = get_irn_mode(left);
4016                                         changed = true;
4017                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4018                                 }
4019                         }
4020                 }
4021         }
4022
4023         /*
4024          * Optimize -a CMP -b into b CMP a.
4025          * This works only for modes where unary Minus cannot Overflow.
4026          * Note that two-complement integers can Overflow so it will NOT work.
4027          */
4028         if (!mode_overflow_on_unary_Minus(mode) &&
4029                         is_Minus(left) && is_Minus(right)) {
4030                 left     = get_Minus_op(left);
4031                 right    = get_Minus_op(right);
4032                 relation = get_inversed_relation(relation);
4033                 changed  = true;
4034                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4035         }
4036
4037         /* remove operation on both sides if possible */
4038         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4039                 /*
4040                  * The following operations are NOT safe for floating point operations, for instance
4041                  * 1.0 + inf == 2.0 + inf, =/=> x == y
4042                  */
4043                 if (mode_is_int(mode)) {
4044                         unsigned lop = get_irn_opcode(left);
4045
4046                         if (lop == get_irn_opcode(right)) {
4047                                 ir_node *ll, *lr, *rl, *rr;
4048
4049                                 /* same operation on both sides, try to remove */
4050                                 switch (lop) {
4051                                 case iro_Not:
4052                                 case iro_Minus:
4053                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4054                                         left  = get_unop_op(left);
4055                                         right = get_unop_op(right);
4056                                         changed = true;
4057                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4058                                         break;
4059                                 case iro_Add:
4060                                         ll = get_Add_left(left);
4061                                         lr = get_Add_right(left);
4062                                         rl = get_Add_left(right);
4063                                         rr = get_Add_right(right);
4064
4065                                         if (ll == rl) {
4066                                                 /* X + a CMP X + b ==> a CMP b */
4067                                                 left  = lr;
4068                                                 right = rr;
4069                                                 changed = true;
4070                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4071                                         } else if (ll == rr) {
4072                                                 /* X + a CMP b + X ==> a CMP b */
4073                                                 left  = lr;
4074                                                 right = rl;
4075                                                 changed = true;
4076                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4077                                         } else if (lr == rl) {
4078                                                 /* a + X CMP X + b ==> a CMP b */
4079                                                 left  = ll;
4080                                                 right = rr;
4081                                                 changed = true;
4082                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4083                                         } else if (lr == rr) {
4084                                                 /* a + X CMP b + X ==> a CMP b */
4085                                                 left  = ll;
4086                                                 right = rl;
4087                                                 changed = true;
4088                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4089                                         }
4090                                         break;
4091                                 case iro_Sub:
4092                                         ll = get_Sub_left(left);
4093                                         lr = get_Sub_right(left);
4094                                         rl = get_Sub_left(right);
4095                                         rr = get_Sub_right(right);
4096
4097                                         if (ll == rl) {
4098                                                 /* X - a CMP X - b ==> a CMP b */
4099                                                 left  = lr;
4100                                                 right = rr;
4101                                                 changed = true;
4102                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4103                                         } else if (lr == rr) {
4104                                                 /* a - X CMP b - X ==> a CMP b */
4105                                                 left  = ll;
4106                                                 right = rl;
4107                                                 changed = true;
4108                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4109                                         }
4110                                         break;
4111                                 case iro_Rotl:
4112                                         if (get_Rotl_right(left) == get_Rotl_right(right)) {
4113                                                 /* a ROTL X CMP b ROTL X ==> a CMP b */
4114                                                 left  = get_Rotl_left(left);
4115                                                 right = get_Rotl_left(right);
4116                                                 changed = true;
4117                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4118                                         }
4119                                         break;
4120                                 default:
4121                                         break;
4122                                 }
4123                         }
4124
4125                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
4126                         if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4127                                 ir_node *ll = get_binop_left(left);
4128                                 ir_node *lr = get_binop_right(left);
4129
4130                                 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4131                                         ir_node *tmp = ll;
4132                                         ll = lr;
4133                                         lr = tmp;
4134                                 }
4135                                 if (ll == right) {
4136                                         ir_graph *irg = get_irn_irg(n);
4137                                         left     = lr;
4138                                         right   = create_zero_const(irg, mode);
4139                                         changed = true;
4140                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4141                                 }
4142                         }
4143                         if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4144                                 ir_node *rl = get_binop_left(right);
4145                                 ir_node *rr = get_binop_right(right);
4146
4147                                 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4148                                         ir_node *tmp = rl;
4149                                         rl = rr;
4150                                         rr = tmp;
4151                                 }
4152                                 if (rl == left) {
4153                                         ir_graph *irg = get_irn_irg(n);
4154                                         left     = rr;
4155                                         right   = create_zero_const(irg, mode);
4156                                         changed = true;
4157                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4158                                 }
4159                         }
4160
4161                         if (is_And(left) && is_Const(right)) {
4162                                 ir_node *ll = get_binop_left(left);
4163                                 ir_node *lr = get_binop_right(left);
4164                                 if (is_Shr(ll) && is_Const(lr)) {
4165                                         /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4166                                         ir_node *block = get_nodes_block(n);
4167                                         ir_mode *mode = get_irn_mode(left);
4168
4169                                         ir_node *llr = get_Shr_right(ll);
4170                                         if (is_Const(llr)) {
4171                                                 dbg_info *dbg = get_irn_dbg_info(left);
4172                                                 ir_graph *irg = get_irn_irg(left);
4173
4174                                                 ir_tarval *c1    = get_Const_tarval(llr);
4175                                                 ir_tarval *c2    = get_Const_tarval(lr);
4176                                                 ir_tarval *c3    = get_Const_tarval(right);
4177                                                 ir_tarval *mask  = tarval_shl(c2, c1);
4178                                                 ir_tarval *value = tarval_shl(c3, c1);
4179
4180                                                 left  = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4181                                                 right = new_r_Const(irg, value);
4182                                                 changed = true;
4183                                         }
4184                                 }
4185                         }
4186                         /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4187                          * cases */
4188                         if (is_Const(right) && is_Const_null(right) &&
4189                             (is_Eor(left) || is_Or_Eor_Add(left))) {
4190                                 right = get_Eor_right(left);
4191                                 left  = get_Eor_left(left);
4192                                 changed = true;
4193                         }
4194                 }
4195         }
4196
4197         if (mode_is_int(mode) && is_And(left)) {
4198                 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4199                  * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4200                 if (relation == ir_relation_equal
4201                 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4202                 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4203                         ir_node *and0 = get_And_left(left);
4204                         ir_node *and1 = get_And_right(left);
4205                         if (and1 == right) {
4206                                 ir_node *tmp = and0;
4207                                 and0 = and1;
4208                                 and1 = tmp;
4209                         }
4210                         if (and0 == right && is_single_bit(and0)) {
4211                                 ir_graph *irg = get_irn_irg(n);
4212                                 relation =
4213                                         relation == ir_relation_equal ? ir_relation_less_greater
4214                                                                       : ir_relation_equal;
4215                                 right = create_zero_const(irg, mode);
4216                                 changed |= 1;
4217                                 goto is_bittest;
4218                         }
4219                 }
4220
4221                 if (is_Const(right) && is_Const_null(right) &&
4222                     (relation == ir_relation_equal
4223                     || (relation == ir_relation_less_greater)
4224                     || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4225 is_bittest: {
4226                         /* instead of flipping the bit before the bit-test operation negate
4227                          * pnc */
4228                         ir_node *and0 = get_And_left(left);
4229                         ir_node *and1 = get_And_right(left);
4230                         if (is_Const(and1)) {
4231                                 ir_tarval *tv = get_Const_tarval(and1);
4232                                 if (tarval_is_single_bit(tv)) {
4233                                         ir_node *flipped = flips_bit(and0, tv);
4234                                         if (flipped != NULL) {
4235                                                 dbg_info *dbgi  = get_irn_dbg_info(left);
4236                                                 ir_node  *block = get_nodes_block(left);
4237                                                 relation = get_negated_relation(relation);
4238                                                 left = new_rd_And(dbgi, block, flipped, and1, mode);
4239                                                 changed |= 1;
4240                                         }
4241                                 }
4242                         }
4243                         }
4244                 }
4245         }
4246
4247         /* replace mode_b compares with ands/ors */
4248         if (mode == mode_b) {
4249                 ir_node  *block = get_nodes_block(n);
4250                 ir_node  *bres;
4251
4252                 switch (relation) {
4253                         case ir_relation_less_equal:
4254                                 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4255                                 break;
4256                         case ir_relation_less:
4257                                 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4258                                 break;
4259                         case ir_relation_greater_equal:
4260                                 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4261                                 break;
4262                         case ir_relation_greater:
4263                                 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4264                                 break;
4265                         case ir_relation_less_greater:
4266                                 bres = new_r_Eor(block, left, right, mode_b);
4267                                 break;
4268                         case ir_relation_equal:
4269                                 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4270                                 break;
4271                         default:
4272 #ifdef DEBUG_libfirm
4273                                 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4274 #endif
4275                                 bres = NULL;
4276                 }
4277                 if (bres != NULL) {
4278                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4279                         return bres;
4280                 }
4281         }
4282
4283         /*
4284          * First step: normalize the compare op
4285          * by placing the constant on the right side
4286          * or moving the lower address node to the left.
4287          */
4288         if (!operands_are_normalized(left, right)) {
4289                 ir_node *t = left;
4290                 left  = right;
4291                 right = t;
4292
4293                 relation = get_inversed_relation(relation);
4294                 changed  = true;
4295         }
4296
4297         /*
4298          * Second step: Try to reduce the magnitude
4299          * of a constant. This may help to generate better code
4300          * later and may help to normalize more compares.
4301          * Of course this is only possible for integer values.
4302          */
4303         tv = value_of(right);
4304         if (tv != tarval_bad) {
4305                 ir_mode *mode = get_irn_mode(right);
4306
4307                 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4308                  *   cmp(ct,c2) | cmp(cf,c2) | result
4309                  *   -----------|------------|--------
4310                  *   true       | true       | True
4311                  *   false      | false      | False
4312                  *   true       | false      | x
4313                  *   false      | true       | not(x)
4314                  */
4315                 if (is_Mux(left)) {
4316                         ir_node *mux_true  = get_Mux_true(left);
4317                         ir_node *mux_false = get_Mux_false(left);
4318                         if (is_Const(mux_true) && is_Const(mux_false)) {
4319                                 /* we can fold true/false constant separately */
4320                                 ir_tarval *tv_true  = get_Const_tarval(mux_true);
4321                                 ir_tarval *tv_false = get_Const_tarval(mux_false);
4322                                 ir_relation r_true  = tarval_cmp(tv_true, tv);
4323                                 ir_relation r_false = tarval_cmp(tv_false, tv);
4324                                 if (r_true != ir_relation_false
4325                                     || r_false != ir_relation_false) {
4326                                         bool rel_true  = (r_true & relation)  != 0;
4327                                         bool rel_false = (r_false & relation) != 0;
4328                                         ir_node *cond = get_Mux_sel(left);
4329                                         if (rel_true == rel_false) {
4330                                                 relation = rel_true ? ir_relation_true
4331                                                                     : ir_relation_false;
4332                                         } else if (rel_true) {
4333                                                 return cond;
4334                                         } else {
4335                                                 dbg_info *dbgi  = get_irn_dbg_info(n);
4336                                                 ir_node  *block = get_nodes_block(n);
4337                                                 ir_node  *notn  = new_rd_Not(dbgi, block, cond, mode_b);
4338                                                 return notn;
4339                                         }
4340                                 }
4341                         }
4342                 }
4343
4344                 /* TODO extend to arbitrary constants */
4345                 if (is_Conv(left) && tarval_is_null(tv)) {
4346                         ir_node *op      = get_Conv_op(left);
4347                         ir_mode *op_mode = get_irn_mode(op);
4348
4349                         /*
4350                          * UpConv(x) REL 0  ==> x REL 0
4351                          * Don't do this for float values as it's unclear whether it is a
4352                          * win. (on the other side it makes detection/creation of fabs hard)
4353                          */
4354                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4355                             ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4356                                  mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4357                                 !mode_is_float(mode)) {
4358                                 tv   = get_mode_null(op_mode);
4359                                 left = op;
4360                                 mode = op_mode;
4361                                 changedc = true;
4362                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4363                         }
4364                 }
4365
4366                 if (tv != tarval_bad) {
4367                         /* the following optimization is possible on modes without Overflow
4368                          * on Unary Minus or on == and !=:
4369                          * -a CMP c  ==>  a swap(CMP) -c
4370                          *
4371                          * Beware: for two-complement Overflow may occur, so only == and != can
4372                          * be optimized, see this:
4373                          * -MININT < 0 =/=> MININT > 0 !!!
4374                          */
4375                         if (is_Minus(left) &&
4376                                 (!mode_overflow_on_unary_Minus(mode) ||
4377                                 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4378                                 tv = tarval_neg(tv);
4379
4380                                 if (tv != tarval_bad) {
4381                                         left = get_Minus_op(left);
4382                                         relation = get_inversed_relation(relation);
4383                                         changedc = true;
4384                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4385                                 }
4386                         } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4387                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
4388                                 tv = tarval_not(tv);
4389
4390                                 if (tv != tarval_bad) {
4391                                         left = get_Not_op(left);
4392                                         changedc = true;
4393                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4394                                 }
4395                         }
4396
4397                         /* for integer modes, we have more */
4398                         if (mode_is_int(mode) && !is_Const(left)) {
4399                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
4400                                 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4401                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4402                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4403
4404                                         if (tv != tarval_bad) {
4405                                                 relation ^= ir_relation_equal;
4406                                                 changedc = true;
4407                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4408                                         }
4409                                 }
4410                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4411                                 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4412                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4413                                         tv = tarval_add(tv, get_mode_one(mode));
4414
4415                                         if (tv != tarval_bad) {
4416                                                 relation ^= ir_relation_equal;
4417                                                 changedc = true;
4418                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4419                                         }
4420                                 }
4421
4422                                 /* the following reassociations work only for == and != */
4423                                 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4424                                         if (tv != tarval_bad) {
4425                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4426                                                 if (is_Sub(left)) {
4427                                                         ir_node *c1 = get_Sub_right(left);
4428                                                         ir_tarval *tv2 = value_of(c1);
4429
4430                                                         if (tv2 != tarval_bad) {
4431                                                                 tv2 = tarval_add(tv, value_of(c1));
4432
4433                                                                 if (tv2 != tarval_bad) {
4434                                                                         left    = get_Sub_left(left);
4435                                                                         tv      = tv2;
4436                                                                         changedc = true;
4437                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4438                                                                 }
4439                                                         }
4440                                                 }
4441                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4442                                                 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4443                                                         ir_node *a_l = get_binop_left(left);
4444                                                         ir_node *a_r = get_binop_right(left);
4445                                                         ir_node *a;
4446                                                         ir_tarval *tv2;
4447
4448                                                         if (is_Const(a_l)) {
4449                                                                 a = a_r;
4450                                                                 tv2 = value_of(a_l);
4451                                                         } else {
4452                                                                 a = a_l;
4453                                                                 tv2 = value_of(a_r);
4454                                                         }
4455
4456                                                         if (tv2 != tarval_bad) {
4457                                                                 tv2 = tarval_sub(tv, tv2, NULL);
4458
4459                                                                 if (tv2 != tarval_bad) {
4460                                                                         left    = a;
4461                                                                         tv      = tv2;
4462                                                                         changedc = true;
4463                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4464                                                                 }
4465                                                         }
4466                                                 }
4467                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4468                                                 else if (is_Minus(left)) {
4469                                                         ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4470
4471                                                         if (tv2 != tarval_bad) {
4472                                                                 left    = get_Minus_op(left);
4473                                                                 tv      = tv2;
4474                                                                 changedc = true;
4475                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4476                                                         }
4477                                                 }
4478                                         }
4479                                 }
4480                         }
4481
4482                         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4483                                 switch (get_irn_opcode(left)) {
4484                                         ir_node *c1;
4485
4486                                 case iro_And:
4487                                         c1 = get_And_right(left);
4488                                         if (is_Const(c1)) {
4489                                                 /*
4490                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4491                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4492                                                  */
4493                                                 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4494                                                 if (mask != tv) {
4495                                                         /* TODO: move to constant evaluation */
4496                                                         ir_graph *irg = get_irn_irg(n);
4497                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4498                                                         c1 = new_r_Const(irg, tv);
4499                                                         DBG_OPT_CSTEVAL(n, c1);
4500                                                         return c1;
4501                                                 }
4502
4503                                                 if (tarval_is_single_bit(tv)) {
4504                                                         /*
4505                                                          * optimization for AND:
4506                                                          * Optimize:
4507                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4508                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4509                                                          *
4510                                                          * if C is a single Bit constant.
4511                                                          */
4512
4513                                                         /* check for Constant's match. We have check hare the tarvals,
4514                                                            because our const might be changed */
4515                                                         if (get_Const_tarval(c1) == tv) {
4516                                                                 /* fine: do the transformation */
4517                                                                 tv = get_mode_null(get_tarval_mode(tv));
4518                                                                 relation ^= ir_relation_less_equal_greater;
4519                                                                 changedc = true;
4520                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4521                                                         }
4522                                                 }
4523                                         }
4524                                         break;
4525                                 case iro_Or:
4526                                         c1 = get_Or_right(left);
4527                                         if (is_Const(c1) && tarval_is_null(tv)) {
4528                                                 /*
4529                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4530                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4531                                                  */
4532                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4533                                                         /* TODO: move to constant evaluation */
4534                                                         ir_graph *irg = get_irn_irg(n);
4535                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4536                                                         c1 = new_r_Const(irg, tv);
4537                                                         DBG_OPT_CSTEVAL(n, c1);
4538                                                         return c1;
4539                                                 }
4540                                         }
4541                                         break;
4542                                 case iro_Shl:
4543                                         /*
4544                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4545                                          *                             FALSE                       else
4546                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4547                                          *                             TRUE                        else
4548                                          */
4549                                         c1 = get_Shl_right(left);
4550                                         if (is_Const(c1)) {
4551                                                 ir_graph  *irg    = get_irn_irg(c1);
4552                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4553                                                 ir_mode   *mode   = get_irn_mode(left);
4554                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4555                                                 ir_tarval *amask  = tarval_shr(minus1, tv1);
4556                                                 ir_tarval *cmask  = tarval_shl(minus1, tv1);
4557                                                 ir_node   *sl, *blk;
4558
4559                                                 if (tarval_and(tv, cmask) != tv) {
4560                                                         /* condition not met */
4561                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4562                                                         c1 = new_r_Const(irg, tv);
4563                                                         DBG_OPT_CSTEVAL(n, c1);
4564                                                         return c1;
4565                                                 }
4566                                                 sl   = get_Shl_left(left);
4567                                                 blk  = get_nodes_block(n);
4568                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4569                                                 tv   = tarval_shr(tv, tv1);
4570                                                 changedc = true;
4571                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4572                                         }
4573                                         break;
4574                                 case iro_Shr:
4575                                         /*
4576                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4577                                          *                             FALSE                       else
4578                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4579                                          *                             TRUE                        else
4580                                          */
4581                                         c1 = get_Shr_right(left);
4582                                         if (is_Const(c1)) {
4583                                                 ir_graph  *irg    = get_irn_irg(c1);
4584                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4585                                                 ir_mode   *mode   = get_irn_mode(left);
4586                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4587                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4588                                                 ir_tarval *cmask  = tarval_shr(minus1, tv1);
4589                                                 ir_node   *sl, *blk;
4590
4591                                                 if (tarval_and(tv, cmask) != tv) {
4592                                                         /* condition not met */
4593                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4594                                                         c1 = new_r_Const(irg, tv);
4595                                                         DBG_OPT_CSTEVAL(n, c1);
4596                                                         return c1;
4597                                                 }
4598                                                 sl   = get_Shr_left(left);
4599                                                 blk  = get_nodes_block(n);
4600                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4601                                                 tv   = tarval_shl(tv, tv1);
4602                                                 changedc = true;
4603                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4604                                         }
4605                                         break;
4606                                 case iro_Shrs:
4607                                         /*
4608                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4609                                          *                             FALSE                       else
4610                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4611                                          *                             TRUE                        else
4612                                          */
4613                                         c1 = get_Shrs_right(left);
4614                                         if (is_Const(c1)) {
4615                                                 ir_graph  *irg    = get_irn_irg(c1);
4616                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4617                                                 ir_mode   *mode   = get_irn_mode(left);
4618                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4619                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4620                                                 ir_tarval *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4621                                                 ir_node *sl, *blk;
4622
4623                                                 cond = tarval_sub(cond, tv1, NULL);
4624                                                 cond = tarval_shrs(tv, cond);
4625
4626                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4627                                                         /* condition not met */
4628                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4629                                                         c1 = new_r_Const(irg, tv);
4630                                                         DBG_OPT_CSTEVAL(n, c1);
4631                                                         return c1;
4632                                                 }
4633                                                 sl   = get_Shrs_left(left);
4634                                                 blk  = get_nodes_block(n);
4635                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4636                                                 tv   = tarval_shl(tv, tv1);
4637                                                 changedc = true;
4638                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4639                                         }
4640                                         break;
4641                                 }
4642                         }
4643                 }
4644         }
4645
4646         if (changedc) {     /* need a new Const */
4647                 ir_graph *irg = get_irn_irg(n);
4648                 right = new_r_Const(irg, tv);
4649                 changed = true;
4650         }
4651
4652         if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4653                 ir_node *op = get_Proj_pred(left);
4654
4655                 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4656                         ir_node *c = get_binop_right(op);
4657
4658                         if (is_Const(c)) {
4659                                 ir_tarval *tv = get_Const_tarval(c);
4660
4661                                 if (tarval_is_single_bit(tv)) {
4662                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4663                                         ir_node *v    = get_binop_left(op);
4664                                         ir_node *blk  = get_nodes_block(op);
4665                                         ir_graph *irg = get_irn_irg(op);
4666                                         ir_mode *mode = get_irn_mode(v);
4667
4668                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4669                                         left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4670                                         changed = true;
4671                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4672                                 }
4673                         }
4674                 }
4675         }
4676
4677         if (changed) {
4678                 dbg_info *dbgi  = get_irn_dbg_info(n);
4679                 ir_node  *block = get_nodes_block(n);
4680
4681                 /* create a new compare */
4682                 n = new_rd_Cmp(dbgi, block, left, right, relation);
4683         }
4684
4685         return n;
4686 }
4687
4688 /**
4689  * Optimize CopyB(mem, x, x) into a Nop.
4690  */
4691 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4692 {
4693         ir_node *copyb = get_Proj_pred(proj);
4694         ir_node *a     = get_CopyB_dst(copyb);
4695         ir_node *b     = get_CopyB_src(copyb);
4696
4697         if (a == b) {
4698                 switch (get_Proj_proj(proj)) {
4699                 case pn_CopyB_X_regular:
4700                         /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4701                         DBG_OPT_EXC_REM(proj);
4702                         proj = new_r_Jmp(get_nodes_block(copyb));
4703                         break;
4704                 case pn_CopyB_X_except: {
4705                         ir_graph *irg = get_irn_irg(proj);
4706                         DBG_OPT_EXC_REM(proj);
4707                         proj = new_r_Bad(irg, mode_X);
4708                         break;
4709                 }
4710                 default:
4711                         break;
4712                 }
4713         }
4714         return proj;
4715 }
4716
4717 /**
4718  * Does all optimizations on nodes that must be done on its Projs
4719  * because of creating new nodes.
4720  */
4721 static ir_node *transform_node_Proj(ir_node *proj)
4722 {
4723         ir_node *n = get_Proj_pred(proj);
4724
4725         if (n->op->ops.transform_node_Proj)
4726                 return n->op->ops.transform_node_Proj(proj);
4727         return proj;
4728 }
4729
4730 /**
4731  * Test whether a block is unreachable
4732  * Note: That this only returns true when
4733  * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4734  * This is important, as you easily end up producing invalid constructs in the
4735  * unreachable code when optimizing away edges into the unreachable code.
4736  * So only set this flag when you iterate localopts to the fixpoint.
4737  * When you reach the fixpoint then all unreachable code is dead
4738  * (= can't be reached by firm edges) and you won't see the invalid constructs
4739  * anymore.
4740  */
4741 static bool is_block_unreachable(const ir_node *block)
4742 {
4743         const ir_graph *irg = get_irn_irg(block);
4744         if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4745                 return false;
4746         return get_Block_dom_depth(block) < 0;
4747 }
4748
4749 static ir_node *transform_node_Block(ir_node *block)
4750 {
4751         ir_graph *irg   = get_irn_irg(block);
4752         int       arity = get_irn_arity(block);
4753         ir_node  *bad   = NULL;
4754         int       i;
4755
4756         if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4757                 return block;
4758
4759         for (i = 0; i < arity; ++i) {
4760                 ir_node *const pred = get_Block_cfgpred(block, i);
4761                 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4762                         continue;
4763                 if (bad == NULL)
4764                         bad = new_r_Bad(irg, mode_X);
4765                 set_irn_n(block, i, bad);
4766         }
4767
4768         return block;
4769 }
4770
4771 static ir_node *transform_node_Phi(ir_node *phi)
4772 {
4773         int       n     = get_irn_arity(phi);
4774         ir_mode  *mode  = get_irn_mode(phi);
4775         ir_node  *block = get_nodes_block(phi);
4776         ir_graph *irg   = get_irn_irg(phi);
4777         ir_node  *bad   = NULL;
4778         int       i;
4779
4780         /* Set phi-operands for bad-block inputs to bad */
4781         for (i = 0; i < n; ++i) {
4782                 if (!is_Bad(get_Phi_pred(phi, i))) {
4783                         ir_node *pred = get_Block_cfgpred(block, i);
4784                         if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4785                                 if (bad == NULL)
4786                                         bad = new_r_Bad(irg, mode);
4787                                 set_irn_n(phi, i, bad);
4788                         }
4789                 }
4790         }
4791
4792         /* Move Pin nodes down through Phi nodes. */
4793         if (mode == mode_M) {
4794                 n = get_irn_arity(phi);
4795
4796                 /* Beware of Phi0 */
4797                 if (n > 0) {
4798                         ir_node **in;
4799                         ir_node  *new_phi;
4800                         bool      has_pin = false;
4801
4802                         NEW_ARR_A(ir_node *, in, n);
4803
4804                         for (i = 0; i < n; ++i) {
4805                                 ir_node *pred = get_irn_n(phi, i);
4806
4807                                 if (is_Pin(pred)) {
4808                                         in[i]   = get_Pin_op(pred);
4809                                         has_pin = true;
4810                                 } else if (is_Bad(pred)) {
4811                                         in[i] = pred;
4812                                 } else {
4813                                         return phi;
4814                                 }
4815                         }
4816
4817                         if (!has_pin)
4818                                 return phi;
4819
4820                         /* Move the Pin nodes "behind" the Phi. */
4821                         new_phi = new_r_Phi(block, n, in, mode_M);
4822                         return new_r_Pin(block, new_phi);
4823                 }
4824         }
4825         /* Move Confirms down through Phi nodes. */
4826         else if (mode_is_reference(mode)) {
4827                 n = get_irn_arity(phi);
4828
4829                 /* Beware of Phi0 */
4830                 if (n > 0) {
4831                         ir_node    *pred = get_irn_n(phi, 0);
4832                         ir_node    *bound, *new_phi, **in;
4833                         ir_relation relation;
4834                         bool        has_confirm = false;
4835
4836                         if (! is_Confirm(pred))
4837                                 return phi;
4838
4839                         bound    = get_Confirm_bound(pred);
4840                         relation = get_Confirm_relation(pred);
4841
4842                         NEW_ARR_A(ir_node *, in, n);
4843                         in[0] = get_Confirm_value(pred);
4844
4845                         for (i = 1; i < n; ++i) {
4846                                 pred = get_irn_n(phi, i);
4847
4848                                 if (is_Confirm(pred) &&
4849                                                 get_Confirm_bound(pred) == bound &&
4850                                                 get_Confirm_relation(pred) == relation) {
4851                                         in[i]       = get_Confirm_value(pred);
4852                                         has_confirm = true;
4853                                 } else if (is_Bad(pred)) {
4854                                         in[i] = pred;
4855                                 } else {
4856                                         return phi;
4857                                 }
4858                         }
4859
4860                         if (!has_confirm)
4861                                 return phi;
4862
4863                         /* move the Confirm nodes "behind" the Phi */
4864                         new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4865                         return new_r_Confirm(block, new_phi, bound, relation);
4866                 }
4867         }
4868         return phi;
4869 }
4870
4871 /**
4872  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4873  *
4874  * Should be moved to reassociation?
4875  */
4876 static ir_node *transform_node_shift(ir_node *n)
4877 {
4878         ir_node *left, *right;
4879         ir_mode *mode;
4880         ir_mode *count_mode;
4881         ir_tarval *tv1, *tv2, *res;
4882         ir_node *in[2], *irn, *block;
4883         ir_graph *irg;
4884         int       modulo_shf;
4885
4886         left = get_binop_left(n);
4887
4888         /* different operations */
4889         if (get_irn_op(left) != get_irn_op(n))
4890                 return n;
4891
4892         right = get_binop_right(n);
4893         tv1   = value_of(right);
4894         if (tv1 == tarval_bad)
4895                 return n;
4896
4897         tv2 = value_of(get_binop_right(left));
4898         if (tv2 == tarval_bad)
4899                 return n;
4900
4901         count_mode = get_tarval_mode(tv1);
4902         if (get_tarval_mode(tv2) != count_mode) {
4903                 /* TODO: search bigger mode or something and convert... */
4904                 return n;
4905         }
4906
4907         mode       = get_irn_mode(n);
4908         modulo_shf = get_mode_modulo_shift(mode);
4909
4910         if (modulo_shf > 0) {
4911                 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4912
4913                 /* I'm not so sure what happens in one complement... */
4914                 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4915                 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4916                  * above will be invalid) */
4917                 assert(modulo_shf<=0 || is_po2(modulo_shf));
4918
4919                 tv1 = tarval_and(tv1, modulo_mask);
4920                 tv2 = tarval_and(tv2, modulo_mask);
4921         }
4922         res = tarval_add(tv1, tv2);
4923         irg = get_irn_irg(n);
4924
4925         /* beware: a simple replacement works only, if res < modulo shift */
4926         if (is_Rotl(n)) {
4927                 int        bits   = get_mode_size_bits(mode);
4928                 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4929                 res = tarval_mod(res, modulo);
4930         } else {
4931                 long       bits      = get_mode_size_bits(mode);
4932                 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4933
4934                 /* shifting too much */
4935                 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4936                         if (is_Shrs(n)) {
4937                                 ir_node  *block = get_nodes_block(n);
4938                                 dbg_info *dbgi  = get_irn_dbg_info(n);
4939                                 ir_mode  *smode = get_irn_mode(right);
4940                                 ir_node  *cnst  = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4941                                 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4942                         }
4943
4944                         return new_r_Const(irg, get_mode_null(mode));
4945                 }
4946         }
4947
4948         /* ok, we can replace it */
4949         assert(modulo_shf >= (int) get_mode_size_bits(mode));
4950         block = get_nodes_block(n);
4951
4952         in[0] = get_binop_left(left);
4953         in[1] = new_r_Const(irg, res);
4954
4955         irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4956
4957         DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4958
4959         return irn;
4960 }
4961
4962 /**
4963  * normalisation:
4964  *    (x << c1) >> c2  <=>  x OP (c2-c1) & ((-1 << c1) >> c2)
4965  *    also:
4966  *    (x >> c1) << c2  <=>  x OP (c2-c1) & ((-1 >> c1) << c2)
4967  *      (also with x >>s c1  when c1>=c2)
4968  */
4969 static ir_node *transform_node_shl_shr(ir_node *n)
4970 {
4971         ir_node   *left;
4972         ir_node   *right = get_binop_right(n);
4973         ir_node   *x;
4974         ir_node   *block;
4975         ir_mode   *mode;
4976         dbg_info  *dbgi;
4977         ir_node   *new_const;
4978         ir_node   *new_shift;
4979         ir_node   *new_and;
4980         ir_tarval *tv_shl;
4981         ir_tarval *tv_shr;
4982         ir_tarval *tv_shift;
4983         ir_tarval *tv_mask;
4984         ir_graph  *irg;
4985         ir_relation relation;
4986         int        need_shrs = 0;
4987
4988         assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4989
4990         if (!is_Const(right))
4991                 return n;
4992
4993         left = get_binop_left(n);
4994         mode = get_irn_mode(n);
4995         if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4996                 ir_node *shr_right = get_binop_right(left);
4997
4998                 if (!is_Const(shr_right))
4999                         return n;
5000
5001                 x      = get_binop_left(left);
5002                 tv_shr = get_Const_tarval(shr_right);
5003                 tv_shl = get_Const_tarval(right);
5004
5005                 if (is_Shrs(left)) {
5006                         /* shrs variant only allowed if c1 >= c2 */
5007                         if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5008                                 return n;
5009
5010                         tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5011                         need_shrs = 1;
5012                 } else {
5013                         tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5014                 }
5015                 tv_mask = tarval_shl(tv_mask, tv_shl);
5016         } else if (is_Shr(n) && is_Shl(left)) {
5017                 ir_node *shl_right = get_Shl_right(left);
5018
5019                 if (!is_Const(shl_right))
5020                         return n;
5021
5022                 x      = get_Shl_left(left);
5023                 tv_shr = get_Const_tarval(right);
5024                 tv_shl = get_Const_tarval(shl_right);
5025
5026                 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5027                 tv_mask = tarval_shr(tv_mask, tv_shr);
5028         } else {
5029                 return n;
5030         }
5031
5032         if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5033                 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5034         }
5035
5036         assert(tv_mask != tarval_bad);
5037         assert(get_tarval_mode(tv_mask) == mode);
5038
5039         block = get_nodes_block(n);
5040         irg   = get_irn_irg(block);
5041         dbgi  = get_irn_dbg_info(n);
5042
5043         relation = tarval_cmp(tv_shl, tv_shr);
5044         if (relation == ir_relation_less || relation == ir_relation_equal) {
5045                 tv_shift  = tarval_sub(tv_shr, tv_shl, NULL);
5046                 new_const = new_r_Const(irg, tv_shift);
5047                 if (need_shrs) {
5048                         new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5049                 } else {
5050                         new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5051                 }
5052         } else {
5053                 assert(relation == ir_relation_greater);
5054                 tv_shift  = tarval_sub(tv_shl, tv_shr, NULL);
5055                 new_const = new_r_Const(irg, tv_shift);
5056                 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5057         }
5058
5059         new_const = new_r_Const(irg, tv_mask);
5060         new_and   = new_rd_And(dbgi, block, new_shift, new_const, mode);
5061
5062         return new_and;
5063 }
5064
5065 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5066 {
5067         ir_mode   *mode      = get_tarval_mode(tv);
5068         ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5069         return tarval_mod(tv, modulo_tv);
5070 }
5071
5072 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5073                                   ir_node *left, ir_node *right, ir_mode *mode);
5074
5075 /**
5076  * Normalisation: if we have a shl/shr with modulo_shift behaviour
5077  * then we can use that to minimize the value of Add(x, const) or
5078  * Sub(Const, x). In particular this often avoids 1 instruction in some
5079  * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5080  * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5081  */
5082 static ir_node *transform_node_shift_modulo(ir_node *n,
5083                                             new_shift_func new_shift)
5084 {
5085         ir_mode  *mode   = get_irn_mode(n);
5086         int       modulo = get_mode_modulo_shift(mode);
5087         ir_node  *newop  = NULL;
5088         ir_mode  *mode_right;
5089         ir_node  *block;
5090         ir_node  *right;
5091         ir_graph *irg;
5092
5093         if (modulo == 0)
5094                 return n;
5095         if (get_mode_arithmetic(mode) != irma_twos_complement)
5096                 return n;
5097         if (!is_po2(modulo))
5098                 return n;
5099
5100         irg        = get_irn_irg(n);
5101         block      = get_nodes_block(n);
5102         right      = get_binop_right(n);
5103         mode_right = get_irn_mode(right);
5104         if (is_Const(right)) {
5105                 ir_tarval *tv     = get_Const_tarval(right);
5106                 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5107
5108                 if (tv_mod == tv)
5109                         return n;
5110
5111                 newop = new_r_Const(irg, tv_mod);
5112         } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5113                 ir_node *add_right = get_binop_right(right);
5114                 if (is_Const(add_right)) {
5115                         ir_tarval *tv     = get_Const_tarval(add_right);
5116                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5117                         ir_node   *newconst;
5118                         if (tv_mod == tv)
5119                                 return n;
5120
5121                         newconst = new_r_Const(irg, tv_mod);
5122                         newop    = new_r_Add(block, get_binop_left(right), newconst,
5123                                              mode_right);
5124                 }
5125         } else if (is_Sub(right)) {
5126                 ir_node *sub_left = get_Sub_left(right);
5127                 if (is_Const(sub_left)) {
5128                         ir_tarval *tv     = get_Const_tarval(sub_left);
5129                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5130                         ir_node  *newconst;
5131                         if (tv_mod == tv)
5132                                 return n;
5133
5134                         newconst = new_r_Const(irg, tv_mod);
5135                         newop    = new_r_Sub(block, newconst, get_Sub_right(right),
5136                                              mode_right);
5137                 }
5138         } else {
5139                 return n;
5140         }
5141
5142         if (newop != NULL) {
5143                 dbg_info *dbgi = get_irn_dbg_info(n);
5144                 ir_node  *left = get_binop_left(n);
5145                 return new_shift(dbgi, block, left, newop, mode);
5146         }
5147         return n;
5148 }
5149
5150 /**
5151  * Transform a Shr.
5152  */
5153 static ir_node *transform_node_Shr(ir_node *n)
5154 {
5155         ir_node *c, *oldn = n;
5156         ir_node *left  = get_Shr_left(n);
5157         ir_node *right = get_Shr_right(n);
5158         ir_mode *mode  = get_irn_mode(n);
5159
5160         HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5161         n = transform_node_shift(n);
5162
5163         if (is_Shr(n))
5164                 n = transform_node_shift_modulo(n, new_rd_Shr);
5165         if (is_Shr(n))
5166                 n = transform_node_shl_shr(n);
5167         if (is_Shr(n))
5168                 n = transform_node_shift_bitop(n);
5169
5170         return n;
5171 }
5172
5173 /**
5174  * Transform a Shrs.
5175  */
5176 static ir_node *transform_node_Shrs(ir_node *n)
5177 {
5178         ir_node  *oldn = n;
5179         ir_node  *a    = get_Shrs_left(n);
5180         ir_node  *b    = get_Shrs_right(n);
5181         ir_mode  *mode = get_irn_mode(n);
5182         ir_node  *c;
5183         vrp_attr *attr;
5184
5185         if (is_oversize_shift(n)) {
5186                 ir_node  *block = get_nodes_block(n);
5187                 dbg_info *dbgi  = get_irn_dbg_info(n);
5188                 ir_mode  *cmode = get_irn_mode(b);
5189                 long      val   = get_mode_size_bits(cmode)-1;
5190                 ir_graph *irg   = get_irn_irg(n);
5191                 ir_node  *cnst  = new_r_Const_long(irg, cmode, val);
5192                 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5193         }
5194
5195         HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5196         n = transform_node_shift(n);
5197         if (n != oldn)
5198                 return n;
5199
5200         n = transform_node_shift_modulo(n, new_rd_Shrs);
5201         if (n != oldn)
5202                 return n;
5203         n = transform_node_shift_bitop(n);
5204         if (n != oldn)
5205                 return n;
5206
5207         /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5208         attr = vrp_get_info(a);
5209         if (attr != NULL) {
5210                 unsigned   bits   = get_mode_size_bits(mode);
5211                 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5212                 ir_tarval *sign   = tarval_shl(get_mode_one(mode), scount);
5213                 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5214                         dbg_info *dbgi  = get_irn_dbg_info(n);
5215                         ir_node  *block = get_nodes_block(n);
5216                         return new_rd_Shr(dbgi, block, a, b, mode);
5217                 }
5218         }
5219
5220         return n;
5221 }
5222
5223 /**
5224  * Transform a Shl.
5225  */
5226 static ir_node *transform_node_Shl(ir_node *n)
5227 {
5228         ir_node *c, *oldn = n;
5229         ir_node *a    = get_Shl_left(n);
5230         ir_node *b    = get_Shl_right(n);
5231         ir_mode *mode = get_irn_mode(n);
5232
5233         HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5234         n = transform_node_shift(n);
5235
5236         if (is_Shl(n))
5237                 n = transform_node_shift_modulo(n, new_rd_Shl);
5238         if (is_Shl(n))
5239                 n = transform_node_shl_shr(n);
5240         if (is_Shl(n))
5241                 n = transform_node_shift_bitop(n);
5242
5243         return n;
5244 }
5245
5246 /**
5247  * Transform a Rotl.
5248  */
5249 static ir_node *transform_node_Rotl(ir_node *n)
5250 {
5251         ir_node *c, *oldn = n;
5252         ir_node *a    = get_Rotl_left(n);
5253         ir_node *b    = get_Rotl_right(n);
5254         ir_mode *mode = get_irn_mode(n);
5255
5256         HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5257         n = transform_node_shift(n);
5258
5259         if (is_Rotl(n))
5260                 n = transform_node_shift_bitop(n);
5261
5262         return n;
5263 }
5264
5265 /**
5266  * returns mode size for may_leave_out_middle_mode
5267  */
5268 static unsigned get_significand_size(ir_mode *mode)
5269 {
5270         const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5271         switch (arithmetic) {
5272         case irma_ieee754:
5273         case irma_x86_extended_float:
5274                 return get_mode_mantissa_size(mode) + 1;
5275         case irma_twos_complement:
5276                 return get_mode_size_bits(mode);
5277         case irma_none:
5278                 panic("Conv node with irma_none mode?");
5279         }
5280         panic("unexpected mode_arithmetic in get_significand_size");
5281 }
5282
5283 /**
5284  * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5285  * as converting from @p m0 to @p m1 and then to @p m2.
5286  * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5287  * gives the following truth table:
5288  * s -> b -> m  : true
5289  * s -> m -> b  : !signed(s) || signed(m)
5290  * m -> b -> s  : true
5291  * m -> s -> b  : false
5292  * b -> s -> m  : false
5293  * b -> m -> s  : true
5294  *
5295  * s -> b -> b  : true
5296  * s -> s -> b  : false
5297  *
5298  * additional float constraints:
5299  * F -> F -> F: fine
5300  * F -> I -> I: signedness of Is must match
5301  * I -> F -> I: signedness of Is must match
5302  * I -> I -> F: signedness of Is must match
5303  * F -> I -> F: bad
5304  * I -> F -> F: fine
5305  * F -> F -> I: fine
5306  * at least 1 float involved: signedness must match
5307  */
5308 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5309 {
5310         int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5311         if (n_floats == 1) {
5312                 /* because overflow gives strange results we don't touch this case */
5313                 return false;
5314         } else if (n_floats == 2 && !mode_is_float(m1)) {
5315                 return false;
5316         }
5317
5318         unsigned size0 = get_significand_size(m0);
5319         unsigned size1 = get_significand_size(m1);
5320         unsigned size2 = get_significand_size(m2);
5321         if (size1 < size2 && size0 >= size1)
5322                 return false;
5323         if (size1 >= size2)
5324                 return true;
5325         return !mode_is_signed(m0) || mode_is_signed(m1);
5326 }
5327
5328 /**
5329  * Transform a Conv.
5330  */
5331 static ir_node *transform_node_Conv(ir_node *n)
5332 {
5333         ir_node *c, *oldn = n;
5334         ir_mode *mode = get_irn_mode(n);
5335         ir_node *a    = get_Conv_op(n);
5336
5337         if (is_Conv(a)) {
5338                 ir_mode *a_mode = get_irn_mode(a);
5339                 ir_node *b      = get_Conv_op(a);
5340                 ir_mode *b_mode = get_irn_mode(b);
5341                 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5342                         dbg_info *dbgi  = get_irn_dbg_info(n);
5343                         ir_node  *block = get_nodes_block(n);
5344                         return new_rd_Conv(dbgi, block, b, mode);
5345                 }
5346         }
5347
5348         if (mode != mode_b && is_const_Phi(a)) {
5349                 /* Do NOT optimize mode_b Conv's, this leads to remaining
5350                  * Phib nodes later, because the conv_b_lower operation
5351                  * is instantly reverted, when it tries to insert a Convb.
5352                  */
5353                 c = apply_conv_on_phi(a, mode);
5354                 if (c) {
5355                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5356                         return c;
5357                 }
5358         }
5359
5360         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5361                 ir_graph *irg = get_irn_irg(n);
5362                 return new_r_Unknown(irg, mode);
5363         }
5364
5365         if (mode_is_reference(mode) &&
5366                 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5367                 is_Add(a)) {
5368                 ir_node *l = get_Add_left(a);
5369                 ir_node *r = get_Add_right(a);
5370                 dbg_info *dbgi = get_irn_dbg_info(a);
5371                 ir_node *block = get_nodes_block(n);
5372                 if (is_Conv(l)) {
5373                         ir_node *lop = get_Conv_op(l);
5374                         if (get_irn_mode(lop) == mode) {
5375                                 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5376                                 n = new_rd_Add(dbgi, block, lop, r, mode);
5377                                 return n;
5378                         }
5379                 }
5380                 if (is_Conv(r)) {
5381                         ir_node *rop = get_Conv_op(r);
5382                         if (get_irn_mode(rop) == mode) {
5383                                 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5384                                 n = new_rd_Add(dbgi, block, l, rop, mode);
5385                                 return n;
5386                         }
5387                 }
5388         }
5389
5390         return n;
5391 }
5392
5393 /**
5394  * Remove dead blocks and nodes in dead blocks
5395  * in keep alive list.  We do not generate a new End node.
5396  */
5397 static ir_node *transform_node_End(ir_node *n)
5398 {
5399         int i, j, n_keepalives = get_End_n_keepalives(n);
5400         ir_node **in;
5401
5402         NEW_ARR_A(ir_node *, in, n_keepalives);
5403
5404         for (i = j = 0; i < n_keepalives; ++i) {
5405                 ir_node *ka = get_End_keepalive(n, i);
5406                 ir_node *block;
5407                 /* no need to keep Bad */
5408                 if (is_Bad(ka))
5409                         continue;
5410                 /* do not keep unreachable code */
5411                 block = is_Block(ka) ? ka : get_nodes_block(ka);
5412                 if (is_block_unreachable(block))
5413                         continue;
5414                 in[j++] = ka;
5415         }
5416         if (j != n_keepalives)
5417                 set_End_keepalives(n, j, in);
5418         return n;
5419 }
5420
5421 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5422 {
5423         if (is_Minus(a) && get_Minus_op(a) == b)
5424                 return true;
5425         if (is_Minus(b) && get_Minus_op(b) == a)
5426                 return true;
5427         if (is_Sub(a) && is_Sub(b)) {
5428                 ir_node *a_left  = get_Sub_left(a);
5429                 ir_node *a_right = get_Sub_right(a);
5430                 ir_node *b_left  = get_Sub_left(b);
5431                 ir_node *b_right = get_Sub_right(b);
5432
5433                 if (a_left == b_right && a_right == b_left)
5434                         return true;
5435         }
5436
5437         return false;
5438 }
5439
5440 static const ir_node *skip_upconv(const ir_node *node)
5441 {
5442         while (is_Conv(node)) {
5443                 ir_mode       *mode    = get_irn_mode(node);
5444                 const ir_node *op      = get_Conv_op(node);
5445                 ir_mode       *op_mode = get_irn_mode(op);
5446                 if (!smaller_mode(op_mode, mode))
5447                         break;
5448                 node = op;
5449         }
5450         return node;
5451 }
5452
5453 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5454                   const ir_node *mux_true)
5455 {
5456         ir_node    *cmp_left;
5457         ir_node    *cmp_right;
5458         ir_mode    *mode;
5459         ir_relation relation;
5460
5461         if (!is_Cmp(sel))
5462                 return 0;
5463
5464         /**
5465          * Note further that these optimization work even for floating point
5466          * with NaN's because -NaN == NaN.
5467          * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5468          * transformations.
5469          */
5470         mode = get_irn_mode(mux_true);
5471         if (mode_honor_signed_zeros(mode))
5472                 return 0;
5473
5474         /* must be <, <=, >=, > */
5475         relation = get_Cmp_relation(sel);
5476         if ((relation & ir_relation_less_greater) == 0)
5477                 return 0;
5478
5479         if (!ir_is_negated_value(mux_true, mux_false))
5480                 return 0;
5481
5482         mux_true  = skip_upconv(mux_true);
5483         mux_false = skip_upconv(mux_false);
5484
5485         /* must be x cmp 0 */
5486         cmp_right = get_Cmp_right(sel);
5487         if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5488                 return 0;
5489
5490         cmp_left = get_Cmp_left(sel);
5491         if (cmp_left == mux_false) {
5492                 if (relation & ir_relation_less) {
5493                         return 1;
5494                 } else {
5495                         assert(relation & ir_relation_greater);
5496                         return -1;
5497                 }
5498         } else if (cmp_left == mux_true) {
5499                 if (relation & ir_relation_less) {
5500                         return -1;
5501                 } else {
5502                         assert(relation & ir_relation_greater);
5503                         return 1;
5504                 }
5505         }
5506
5507         return 0;
5508 }
5509
5510 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5511                        ir_node *mux_true)
5512 {
5513         ir_node *cmp_left = get_Cmp_left(sel);
5514         return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5515 }
5516
5517 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5518                            const ir_node *mux_true)
5519 {
5520         /* this code should return true each time transform_node_Mux would
5521          * optimize the Mux completely away */
5522
5523         ir_mode *mode = get_irn_mode(mux_false);
5524         if (get_mode_arithmetic(mode) == irma_twos_complement
5525             && ir_mux_is_abs(sel, mux_false, mux_true))
5526             return true;
5527
5528         if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5529                 const ir_node *cmp_r = get_Cmp_right(sel);
5530                 const ir_node *cmp_l = get_Cmp_left(sel);
5531                 const ir_node *f     = mux_false;
5532                 const ir_node *t     = mux_true;
5533
5534                 if (is_Const(t) && is_Const_null(t)) {
5535                         t = mux_false;
5536                         f = mux_true;
5537                 }
5538
5539                 if (is_And(cmp_l) && f == cmp_r) {
5540                         ir_node *and_r = get_And_right(cmp_l);
5541                         ir_node *and_l;
5542
5543                         if (and_r == t && is_single_bit(and_r))
5544                                 return true;
5545                         and_l = get_And_left(cmp_l);
5546                         if (and_l == t && is_single_bit(and_l))
5547                                 return true;
5548                 }
5549         }
5550
5551         return false;
5552 }
5553
5554 /**
5555  * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5556  */
5557 static ir_node *transform_Mux_set(ir_node *n)
5558 {
5559         ir_node    *cond = get_Mux_sel(n);
5560         ir_mode    *dest_mode;
5561         ir_mode    *mode;
5562         ir_node    *left;
5563         ir_node    *right;
5564         ir_relation relation;
5565         bool        need_not;
5566         dbg_info   *dbgi;
5567         ir_node    *block;
5568         ir_graph   *irg;
5569         ir_node    *a;
5570         ir_node    *b;
5571         unsigned    bits;
5572         ir_tarval  *tv;
5573         ir_node    *shift_cnt;
5574         ir_node    *res;
5575
5576         if (!is_Cmp(cond))
5577                 return n;
5578         left = get_Cmp_left(cond);
5579         mode = get_irn_mode(left);
5580         if (!mode_is_int(mode) && !mode_is_reference(mode))
5581                 return n;
5582         dest_mode = get_irn_mode(n);
5583         if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5584                 return n;
5585         right     = get_Cmp_right(cond);
5586         relation  = get_Cmp_relation(cond) & ~ir_relation_unordered;
5587         if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5588             && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5589                  && relation != ir_relation_greater))
5590             return n;
5591
5592         need_not = false;
5593         switch (relation) {
5594         case ir_relation_less:
5595                 /* a < b  ->  (a - b) >> 31 */
5596                 a = left;
5597                 b = right;
5598                 break;
5599         case ir_relation_less_equal:
5600                 /* a <= b  -> ~(a - b) >> 31 */
5601                 a        = right;
5602                 b        = left;
5603                 need_not = true;
5604                 break;
5605         case ir_relation_greater:
5606                 /* a > b   -> (b - a) >> 31 */
5607                 a = right;
5608                 b = left;
5609                 break;
5610         case ir_relation_greater_equal:
5611                 /* a >= b   -> ~(a - b) >> 31 */
5612                 a        = left;
5613                 b        = right;
5614                 need_not = true;
5615                 break;
5616         default:
5617                 return n;
5618         }
5619
5620         dbgi      = get_irn_dbg_info(n);
5621         block     = get_nodes_block(n);
5622         irg       = get_irn_irg(block);
5623         bits      = get_mode_size_bits(dest_mode);
5624         tv        = new_tarval_from_long(bits-1, mode_Iu);
5625         shift_cnt = new_rd_Const(dbgi, irg, tv);
5626
5627         if (mode != dest_mode) {
5628                 a = new_rd_Conv(dbgi, block, a, dest_mode);
5629                 b = new_rd_Conv(dbgi, block, b, dest_mode);
5630         }
5631
5632         res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5633         if (need_not) {
5634                 res = new_rd_Not(dbgi, block, res, dest_mode);
5635         }
5636         res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5637         return res;
5638 }
5639
5640 /**
5641  * Optimize a Mux into some simpler cases.
5642  */
5643 static ir_node *transform_node_Mux(ir_node *n)
5644 {
5645         ir_node  *oldn = n;
5646         ir_node  *sel  = get_Mux_sel(n);
5647         ir_mode  *mode = get_irn_mode(n);
5648         ir_node  *t    = get_Mux_true(n);
5649         ir_node  *f    = get_Mux_false(n);
5650         ir_graph *irg  = get_irn_irg(n);
5651
5652         /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5653         if (get_mode_arithmetic(mode) == irma_twos_complement) {
5654                 int abs = ir_mux_is_abs(sel, f, t);
5655                 if (abs != 0) {
5656                         dbg_info *dbgi       = get_irn_dbg_info(n);
5657                         ir_node  *block      = get_nodes_block(n);
5658                         ir_node  *op         = ir_get_abs_op(sel, f, t);
5659                         int       bits       = get_mode_size_bits(mode);
5660                         ir_node  *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5661                         ir_node  *sext       = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5662                         ir_node  *xorn       = new_rd_Eor(dbgi, block, op, sext, mode);
5663                         ir_node  *res;
5664                         if (abs > 0) {
5665                                 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5666                         } else {
5667                                 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5668                         }
5669                         return res;
5670                 }
5671         }
5672
5673         /* first normalization step: try to move a constant to the false side,
5674          * 0 preferred on false side too */
5675         if (is_Cmp(sel) && is_Const(t) &&
5676                         (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5677                 dbg_info *seldbgi = get_irn_dbg_info(sel);
5678                 ir_node  *block   = get_nodes_block(sel);
5679                 ir_relation relation = get_Cmp_relation(sel);
5680                 ir_node *tmp = t;
5681                 t = f;
5682                 f = tmp;
5683
5684                 /* Mux(x, a, b) => Mux(not(x), b, a) */
5685                 relation = get_negated_relation(relation);
5686                 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5687                                 get_Cmp_right(sel), relation);
5688                 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5689         }
5690
5691         if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5692                 n = transform_Mux_set(n);
5693                 if (n != oldn)
5694                         return n;
5695         }
5696
5697         /* the following optimisations create new mode_b nodes, so only do them
5698          * before mode_b lowering */
5699         if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5700                 if (is_Mux(t)) {
5701                         ir_node*  block = get_nodes_block(n);
5702                         ir_node*  c0    = sel;
5703                         ir_node*  c1    = get_Mux_sel(t);
5704                         ir_node*  t1    = get_Mux_true(t);
5705                         ir_node*  f1    = get_Mux_false(t);
5706                         if (f == f1) {
5707                                 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5708                                 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5709                                 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5710                                 return new_r_Mux(block, and_, f1, t1, mode);
5711                         } else if (f == t1) {
5712                                 /* Mux(cond0, Mux(cond1, x, y), x) */
5713                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
5714                                 ir_node* and_    = new_r_And(block, c0, not_c1, mode_b);
5715                                 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5716                                 return new_r_Mux(block, and_, t1, f1, mode);
5717                         }
5718                 } else if (is_Mux(f)) {
5719                         ir_node*  block = get_nodes_block(n);
5720                         ir_node*  c0    = sel;
5721                         ir_node*  c1    = get_Mux_sel(f);
5722                         ir_node*  t1    = get_Mux_true(f);
5723                         ir_node*  f1    = get_Mux_false(f);
5724                         if (t == t1) {
5725                                 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5726                                 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5727                                 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5728                                 return new_r_Mux(block, or_, f1, t1, mode);
5729                         } else if (t == f1) {
5730                                 /* Mux(cond0, x, Mux(cond1, y, x)) */
5731                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
5732                                 ir_node* or_     = new_r_Or(block, c0, not_c1, mode_b);
5733                                 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5734                                 return new_r_Mux(block, or_, t1, f1, mode);
5735                         }
5736                 }
5737
5738                 /* note: after normalization, false can only happen on default */
5739                 if (mode == mode_b) {
5740                         dbg_info *dbg   = get_irn_dbg_info(n);
5741                         ir_node  *block = get_nodes_block(n);
5742
5743                         if (is_Const(t)) {
5744                                 ir_tarval *tv_t = get_Const_tarval(t);
5745                                 if (tv_t == tarval_b_true) {
5746                                         if (is_Const(f)) {
5747                                                 /* Muxb(sel, true, false) = sel */
5748                                                 assert(get_Const_tarval(f) == tarval_b_false);
5749                                                 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5750                                                 return sel;
5751                                         } else {
5752                                                 /* Muxb(sel, true, x) = Or(sel, x) */
5753                                                 n = new_rd_Or(dbg, block, sel, f, mode_b);
5754                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5755                                                 return n;
5756                                         }
5757                                 }
5758                         } else if (is_Const(f)) {
5759                                 ir_tarval *tv_f = get_Const_tarval(f);
5760                                 if (tv_f == tarval_b_true) {
5761                                         /* Muxb(sel, x, true) = Or(Not(sel), x) */
5762                                         ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5763                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5764                                         n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5765                                         return n;
5766                                 } else {
5767                                         /* Muxb(sel, x, false) = And(sel, x) */
5768                                         assert(tv_f == tarval_b_false);
5769                                         n = new_rd_And(dbg, block, sel, t, mode_b);
5770                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5771                                         return n;
5772                                 }
5773                         }
5774                 }
5775         }
5776
5777         if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5778                 ir_relation relation = get_Cmp_relation(sel);
5779                 ir_node    *cmp_r    = get_Cmp_right(sel);
5780                 ir_node    *cmp_l    = get_Cmp_left(sel);
5781                 ir_node    *block    = get_nodes_block(n);
5782
5783                 if (is_And(cmp_l) && f == cmp_r) {
5784                         ir_node *and_r = get_And_right(cmp_l);
5785                         ir_node *and_l;
5786
5787                         if (and_r == t && is_single_bit(and_r)) {
5788                                 if (relation == ir_relation_equal) {
5789                                         /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5790                                         n = new_rd_Eor(get_irn_dbg_info(n),
5791                                                 block, cmp_l, t, mode);
5792                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5793                                 } else {
5794                                         /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5795                                         n = cmp_l;
5796                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5797                                 }
5798                                 return n;
5799                         }
5800                         and_l = get_And_left(cmp_l);
5801                         if (and_l == t && is_single_bit(and_l)) {
5802                                 if (relation == ir_relation_equal) {
5803                                         /* ((1 << n) & a) == 0, (1 << n), 0) */
5804                                         n = new_rd_Eor(get_irn_dbg_info(n),
5805                                                 block, cmp_l, t, mode);
5806                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5807                                 } else {
5808                                         /* ((1 << n) & a) != 0, (1 << n), 0) */
5809                                         n = cmp_l;
5810                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5811                                 }
5812                                 return n;
5813                         }
5814                 }
5815         }
5816
5817         return n;
5818 }
5819
5820 /**
5821  * optimize Sync nodes that have other syncs as input we simply add the inputs
5822  * of the other sync to our own inputs
5823  */
5824 static ir_node *transform_node_Sync(ir_node *n)
5825 {
5826         int arity = get_Sync_n_preds(n);
5827         int i;
5828
5829         for (i = 0; i < arity;) {
5830                 ir_node *pred = get_Sync_pred(n, i);
5831                 int      pred_arity;
5832                 int      j;
5833
5834                 /* Remove Bad predecessors */
5835                 if (is_Bad(pred)) {
5836                         del_Sync_n(n, i);
5837                         --arity;
5838                         continue;
5839                 }
5840
5841                 /* Remove duplicate predecessors */
5842                 for (j = 0; j < i; ++j) {
5843                         if (get_Sync_pred(n, j) == pred) {
5844                                 del_Sync_n(n, i);
5845                                 --arity;
5846                                 break;
5847                         }
5848                 }
5849                 if (j < i)
5850                         continue;
5851
5852                 if (!is_Sync(pred)) {
5853                         ++i;
5854                         continue;
5855                 }
5856
5857                 del_Sync_n(n, i);
5858                 --arity;
5859
5860                 pred_arity = get_Sync_n_preds(pred);
5861                 for (j = 0; j < pred_arity; ++j) {
5862                         ir_node *pred_pred = get_Sync_pred(pred, j);
5863                         int      k;
5864
5865                         for (k = 0;; ++k) {
5866                                 if (k >= arity) {
5867                                         add_irn_n(n, pred_pred);
5868                                         ++arity;
5869                                         break;
5870                                 }
5871                                 if (get_Sync_pred(n, k) == pred_pred)
5872                                         break;
5873                         }
5874                 }
5875         }
5876
5877         if (arity == 0) {
5878                 ir_graph *irg = get_irn_irg(n);
5879                 return new_r_Bad(irg, mode_M);
5880         }
5881         if (arity == 1) {
5882                 return get_Sync_pred(n, 0);
5883         }
5884
5885         /* rehash the sync node */
5886         add_identities(n);
5887         return n;
5888 }
5889
5890 static ir_node *create_load_replacement_tuple(ir_node *n, ir_node *mem,
5891                                               ir_node *res)
5892 {
5893         ir_node  *block = get_nodes_block(n);
5894         ir_graph *irg   = get_irn_irg(n);
5895         ir_node  *in[pn_Load_max+1];
5896         size_t    n_in  = 2;
5897         in[pn_Load_M]   = mem;
5898         in[pn_Load_res] = res;
5899         if (ir_throws_exception(n)) {
5900                 in[pn_Load_X_regular] = new_r_Jmp(block);
5901                 in[pn_Load_X_except]  = new_r_Bad(irg, mode_X);
5902                 n_in                  = 4;
5903                 assert(pn_Load_max == 4);
5904         }
5905         ir_node  *tuple = new_r_Tuple(block, n_in, in);
5906         return tuple;
5907 }
5908
5909 static ir_node *transform_node_Load(ir_node *n)
5910 {
5911         /* don't touch volatile loads */
5912         if (get_Load_volatility(n) == volatility_is_volatile)
5913                 return n;
5914
5915         ir_node *ptr = get_Load_ptr(n);
5916         const ir_node *confirm;
5917         if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5918                 set_irn_pinned(n, op_pin_state_floats);
5919         }
5920
5921         /* if our memory predecessor is a load from the same address, then reuse the
5922          * previous result */
5923         ir_node *mem = get_Load_mem(n);
5924         if (!is_Proj(mem))
5925                 return n;
5926         ir_node *mem_pred = get_Proj_pred(mem);
5927         if (is_Load(mem_pred)) {
5928                 ir_node *pred_load = mem_pred;
5929
5930                 /* conservatively compare the 2 loads. TODO: This could be less strict
5931                  * with fixup code in some situations (like smaller/bigger modes) */
5932                 if (get_Load_ptr(pred_load) != ptr)
5933                         return n;
5934                 if (get_Load_mode(pred_load) != get_Load_mode(n))
5935                         return n;
5936                 /* all combinations of aligned/unaligned pred/n should be fine so we do
5937                  * not compare the unaligned attribute */
5938                 ir_mode  *mode  = get_Load_mode(n);
5939                 ir_node  *res   = new_r_Proj(pred_load, mode, pn_Load_res);
5940                 return create_load_replacement_tuple(n, mem, res);
5941         } else if (is_Store(mem_pred)) {
5942                 ir_node *pred_store = mem_pred;
5943                 ir_node *value      = get_Store_value(pred_store);
5944
5945                 if (get_Store_ptr(pred_store) != ptr)
5946                         return n;
5947                 if (get_irn_mode(value) != get_Load_mode(n))
5948                         return n;
5949                 /* all combinations of aligned/unaligned pred/n should be fine so we do
5950                  * not compare the unaligned attribute */
5951                 return create_load_replacement_tuple(n, mem, value);
5952         }
5953
5954         return n;
5955 }
5956
5957 static ir_node *transform_node_Store(ir_node *n)
5958 {
5959         /* don't touch volatile stores */
5960         if (get_Store_volatility(n) == volatility_is_volatile)
5961                 return n;
5962
5963         ir_node *ptr = get_Store_ptr(n);
5964         const ir_node *confirm;
5965         if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5966                 set_irn_pinned(n, op_pin_state_floats);
5967         }
5968         return n;
5969 }
5970
5971 /**
5972  * optimize a trampoline Call into a direct Call
5973  */
5974 static ir_node *transform_node_Call(ir_node *call)
5975 {
5976         ir_node  *callee = get_Call_ptr(call);
5977         ir_node  *adr, *mem, *res, *bl, **in;
5978         ir_type  *ctp, *mtp, *tp;
5979         ir_graph *irg;
5980         type_dbg_info *tdb;
5981         dbg_info *db;
5982         size_t   i, n_res, n_param;
5983         ir_variadicity var;
5984
5985         if (! is_Proj(callee))
5986                 return call;
5987         callee = get_Proj_pred(callee);
5988         if (! is_Builtin(callee))
5989                 return call;
5990         if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5991                 return call;
5992
5993         mem = get_Call_mem(call);
5994
5995         if (skip_Proj(mem) == callee) {
5996                 /* memory is routed to the trampoline, skip */
5997                 mem = get_Builtin_mem(callee);
5998         }
5999
6000         /* build a new call type */
6001         mtp = get_Call_type(call);
6002         tdb = get_type_dbg_info(mtp);
6003
6004         n_res   = get_method_n_ress(mtp);
6005         n_param = get_method_n_params(mtp);
6006         ctp     = new_d_type_method(n_param + 1, n_res, tdb);
6007
6008         for (i = 0; i < n_res; ++i)
6009                 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6010
6011         NEW_ARR_A(ir_node *, in, n_param + 1);
6012
6013         /* FIXME: we don't need a new pointer type in every step */
6014         irg = get_irn_irg(call);
6015         tp = get_irg_frame_type(irg);
6016         tp = new_type_pointer(tp);
6017         set_method_param_type(ctp, 0, tp);
6018
6019         in[0] = get_Builtin_param(callee, 2);
6020         for (i = 0; i < n_param; ++i) {
6021                 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6022                 in[i + 1] = get_Call_param(call, i);
6023         }
6024         var = get_method_variadicity(mtp);
6025         set_method_variadicity(ctp, var);
6026         /* When we resolve a trampoline, the function must be called by a this-call */
6027         set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6028         set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6029
6030         adr = get_Builtin_param(callee, 1);
6031
6032         db  = get_irn_dbg_info(call);
6033         bl  = get_nodes_block(call);
6034
6035         res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6036         if (get_irn_pinned(call) == op_pin_state_floats)
6037                 set_irn_pinned(res, op_pin_state_floats);
6038         return res;
6039 }
6040
6041 /**
6042  * Tries several [inplace] [optimizing] transformations and returns an
6043  * equivalent node.  The difference to equivalent_node() is that these
6044  * transformations _do_ generate new nodes, and thus the old node must
6045  * not be freed even if the equivalent node isn't the old one.
6046  */
6047 static ir_node *transform_node(ir_node *n)
6048 {
6049         ir_node *old_n;
6050         unsigned iro;
6051 restart:
6052         old_n = n;
6053         iro   = get_irn_opcode_(n);
6054         /* constant expression evaluation / constant folding */
6055         if (get_opt_constant_folding()) {
6056                 /* neither constants nor Tuple values can be evaluated */
6057                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6058                         /* try to evaluate */
6059                         ir_tarval *tv = computed_value(n);
6060                         if (tv != tarval_bad) {
6061                                 /* evaluation was successful -- replace the node. */
6062                                 ir_graph *irg = get_irn_irg(n);
6063
6064                                 n = new_r_Const(irg, tv);
6065
6066                                 DBG_OPT_CSTEVAL(old_n, n);
6067                                 return n;
6068                         }
6069                 }
6070         }
6071
6072         /* remove unnecessary nodes */
6073         if (get_opt_constant_folding() ||
6074                 (iro == iro_Phi)  ||   /* always optimize these nodes. */
6075                 (iro == iro_Id)   ||   /* ... */
6076                 (iro == iro_Proj) ||   /* ... */
6077                 (iro == iro_Block)) {  /* Flags tested local. */
6078                 n = equivalent_node(n);
6079                 if (n != old_n)
6080                         goto restart;
6081         }
6082
6083         /* Some more constant expression evaluation. */
6084         if (get_opt_algebraic_simplification() ||
6085                 (iro == iro_Cond) ||
6086                 (iro == iro_Proj)) {    /* Flags tested local. */
6087                 if (n->op->ops.transform_node != NULL) {
6088                         n = n->op->ops.transform_node(n);
6089                         if (n != old_n) {
6090                                 goto restart;
6091                         }
6092                 }
6093         }
6094
6095         return n;
6096 }
6097
6098 static void register_computed_value_func(ir_op *op, computed_value_func func)
6099 {
6100         assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6101         op->ops.computed_value = func;
6102 }
6103
6104 static void register_computed_value_func_proj(ir_op *op,
6105                                               computed_value_func func)
6106 {
6107         assert(op->ops.computed_value_Proj == NULL
6108             || op->ops.computed_value_Proj == func);
6109         op->ops.computed_value_Proj = func;
6110 }
6111
6112 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6113 {
6114         assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6115         op->ops.equivalent_node = func;
6116 }
6117
6118 static void register_equivalent_node_func_proj(ir_op *op,
6119                                                equivalent_node_func func)
6120 {
6121         assert(op->ops.equivalent_node_Proj == NULL
6122             || op->ops.equivalent_node_Proj == func);
6123         op->ops.equivalent_node_Proj = func;
6124 }
6125
6126 static void register_transform_node_func(ir_op *op, transform_node_func func)
6127 {
6128         assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6129         op->ops.transform_node = func;
6130 }
6131
6132 static void register_transform_node_func_proj(ir_op *op,
6133                                               transform_node_func func)
6134 {
6135         assert(op->ops.transform_node_Proj == NULL
6136             || op->ops.transform_node_Proj == func);
6137         op->ops.transform_node_Proj = func;
6138 }
6139
6140 void ir_register_opt_node_ops(void)
6141 {
6142         register_computed_value_func(op_Add,      computed_value_Add);
6143         register_computed_value_func(op_And,      computed_value_And);
6144         register_computed_value_func(op_Cmp,      computed_value_Cmp);
6145         register_computed_value_func(op_Confirm,  computed_value_Confirm);
6146         register_computed_value_func(op_Const,    computed_value_Const);
6147         register_computed_value_func(op_Conv,     computed_value_Conv);
6148         register_computed_value_func(op_Eor,      computed_value_Eor);
6149         register_computed_value_func(op_Minus,    computed_value_Minus);
6150         register_computed_value_func(op_Mul,      computed_value_Mul);
6151         register_computed_value_func(op_Mux,      computed_value_Mux);
6152         register_computed_value_func(op_Not,      computed_value_Not);
6153         register_computed_value_func(op_Or,       computed_value_Or);
6154         register_computed_value_func(op_Proj,     computed_value_Proj);
6155         register_computed_value_func(op_Rotl,     computed_value_Rotl);
6156         register_computed_value_func(op_Shl,      computed_value_Shl);
6157         register_computed_value_func(op_Shr,      computed_value_Shr);
6158         register_computed_value_func(op_Shrs,     computed_value_Shrs);
6159         register_computed_value_func(op_Sub,      computed_value_Sub);
6160         register_computed_value_func(op_SymConst, computed_value_SymConst);
6161         register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6162         register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6163
6164         register_equivalent_node_func(op_Add,     equivalent_node_Add);
6165         register_equivalent_node_func(op_And,     equivalent_node_And);
6166         register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6167         register_equivalent_node_func(op_Conv,    equivalent_node_Conv);
6168         register_equivalent_node_func(op_Eor,     equivalent_node_Eor);
6169         register_equivalent_node_func(op_Id,      equivalent_node_Id);
6170         register_equivalent_node_func(op_Minus,   equivalent_node_involution);
6171         register_equivalent_node_func(op_Mul,     equivalent_node_Mul);
6172         register_equivalent_node_func(op_Mux,     equivalent_node_Mux);
6173         register_equivalent_node_func(op_Not,     equivalent_node_involution);
6174         register_equivalent_node_func(op_Or,      equivalent_node_Or);
6175         register_equivalent_node_func(op_Phi,     equivalent_node_Phi);
6176         register_equivalent_node_func(op_Proj,    equivalent_node_Proj);
6177         register_equivalent_node_func(op_Rotl,    equivalent_node_left_zero);
6178         register_equivalent_node_func(op_Shl,     equivalent_node_left_zero);
6179         register_equivalent_node_func(op_Shr,     equivalent_node_left_zero);
6180         register_equivalent_node_func(op_Shrs,    equivalent_node_left_zero);
6181         register_equivalent_node_func(op_Sub,     equivalent_node_Sub);
6182         register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6183         register_equivalent_node_func_proj(op_Div,   equivalent_node_Proj_Div);
6184         register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6185
6186         register_transform_node_func(op_Add,    transform_node_Add);
6187         register_transform_node_func(op_And,    transform_node_And);
6188         register_transform_node_func(op_Block,  transform_node_Block);
6189         register_transform_node_func(op_Call,   transform_node_Call);
6190         register_transform_node_func(op_Cmp,    transform_node_Cmp);
6191         register_transform_node_func(op_Cond,   transform_node_Cond);
6192         register_transform_node_func(op_Conv,   transform_node_Conv);
6193         register_transform_node_func(op_Div,    transform_node_Div);
6194         register_transform_node_func(op_End,    transform_node_End);
6195         register_transform_node_func(op_Eor,    transform_node_Eor);
6196         register_transform_node_func(op_Load,   transform_node_Load);
6197         register_transform_node_func(op_Minus,  transform_node_Minus);
6198         register_transform_node_func(op_Mod,    transform_node_Mod);
6199         register_transform_node_func(op_Mul,    transform_node_Mul);
6200         register_transform_node_func(op_Mux,    transform_node_Mux);
6201         register_transform_node_func(op_Not,    transform_node_Not);
6202         register_transform_node_func(op_Or,     transform_node_Or);
6203         register_transform_node_func(op_Phi,    transform_node_Phi);
6204         register_transform_node_func(op_Proj,   transform_node_Proj);
6205         register_transform_node_func(op_Rotl,   transform_node_Rotl);
6206         register_transform_node_func(op_Shl,    transform_node_Shl);
6207         register_transform_node_func(op_Shrs,   transform_node_Shrs);
6208         register_transform_node_func(op_Shr,    transform_node_Shr);
6209         register_transform_node_func(op_Store,  transform_node_Store);
6210         register_transform_node_func(op_Sub,    transform_node_Sub);
6211         register_transform_node_func(op_Switch, transform_node_Switch);
6212         register_transform_node_func(op_Sync,   transform_node_Sync);
6213         register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6214         register_transform_node_func_proj(op_Div,   transform_node_Proj_Div);
6215         register_transform_node_func_proj(op_Load,  transform_node_Proj_Load);
6216         register_transform_node_func_proj(op_Mod,   transform_node_Proj_Mod);
6217         register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6218 }
6219
6220 /* **************** Common Subexpression Elimination **************** */
6221
6222 /** The size of the hash table used, should estimate the number of nodes
6223     in a graph. */
6224 #define N_IR_NODES 512
6225
6226 int identities_cmp(const void *elt, const void *key)
6227 {
6228         ir_node *a = (ir_node *)elt;
6229         ir_node *b = (ir_node *)key;
6230         int i, irn_arity_a;
6231
6232         if (a == b) return 0;
6233
6234         if ((get_irn_op(a) != get_irn_op(b)) ||
6235             (get_irn_mode(a) != get_irn_mode(b))) return 1;
6236
6237         /* compare if a's in and b's in are of equal length */
6238         irn_arity_a = get_irn_arity(a);
6239         if (irn_arity_a != get_irn_arity(b))
6240                 return 1;
6241
6242         /* blocks are never the same */
6243         if (is_Block(a))
6244                 return 1;
6245
6246         if (get_irn_pinned(a) == op_pin_state_pinned) {
6247                 /* for pinned nodes, the block inputs must be equal */
6248                 if (get_nodes_block(a) != get_nodes_block(b))
6249                         return 1;
6250         } else {
6251                 ir_node *block_a = get_nodes_block(a);
6252                 ir_node *block_b = get_nodes_block(b);
6253                 if (! get_opt_global_cse()) {
6254                         /* for block-local CSE both nodes must be in the same Block */
6255                         if (block_a != block_b)
6256                                 return 1;
6257                 } else {
6258                         /* The optimistic approach would be to do nothing here.
6259                          * However doing GCSE optimistically produces a lot of partially dead code which appears
6260                          * to be worse in practice than the missed opportunities.
6261                          * So we use a very conservative variant here and only CSE if 1 value dominates the
6262                          * other. */
6263                         if (!block_dominates(block_a, block_b)
6264                             && !block_dominates(block_b, block_a))
6265                             return 1;
6266                         /* respect the workaround rule: do not move nodes which are only
6267                          * held by keepalive edges */
6268                         if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6269                                 return 1;
6270                 }
6271         }
6272
6273         /* compare a->in[0..ins] with b->in[0..ins] */
6274         for (i = 0; i < irn_arity_a; ++i) {
6275                 ir_node *pred_a = get_irn_n(a, i);
6276                 ir_node *pred_b = get_irn_n(b, i);
6277                 if (pred_a != pred_b) {
6278                         /* if both predecessors are CSE neutral they might be different */
6279                         if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6280                                 return 1;
6281                 }
6282         }
6283
6284         /*
6285          * here, we already now that the nodes are identical except their
6286          * attributes
6287          */
6288         if (a->op->ops.node_cmp_attr)
6289                 return a->op->ops.node_cmp_attr(a, b);
6290
6291         return 0;
6292 }
6293
6294 unsigned ir_node_hash(const ir_node *node)
6295 {
6296         return node->op->ops.hash(node);
6297 }
6298
6299 void new_identities(ir_graph *irg)
6300 {
6301         if (irg->value_table != NULL)
6302                 del_pset(irg->value_table);
6303         irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6304 }
6305
6306 void del_identities(ir_graph *irg)
6307 {
6308         if (irg->value_table != NULL)
6309                 del_pset(irg->value_table);
6310 }
6311
6312 static int cmp_node_nr(const void *a, const void *b)
6313 {
6314         ir_node **p1 = (ir_node**)a;
6315         ir_node **p2 = (ir_node**)b;
6316         long      n1 = get_irn_node_nr(*p1);
6317         long      n2 = get_irn_node_nr(*p2);
6318         return (n1>n2) - (n1<n2);
6319 }
6320
6321 void ir_normalize_node(ir_node *n)
6322 {
6323         if (is_op_commutative(get_irn_op(n))) {
6324                 ir_node *l = get_binop_left(n);
6325                 ir_node *r = get_binop_right(n);
6326
6327                 /* For commutative operators perform  a OP b == b OP a but keep
6328                  * constants on the RIGHT side. This helps greatly in some
6329                  * optimizations.  Moreover we use the idx number to make the form
6330                  * deterministic. */
6331                 if (!operands_are_normalized(l, r)) {
6332                         set_binop_left(n, r);
6333                         set_binop_right(n, l);
6334                         hook_normalize(n);
6335                 }
6336         } else if (is_Sync(n)) {
6337                 /* we assume that most of the time the inputs of a Sync node are already
6338                  * sorted, so check this first as a shortcut */
6339                 bool           ins_sorted = true;
6340                 int            arity      = get_irn_arity(n);
6341                 const ir_node *last       = get_irn_n(n, 0);
6342                 int      i;
6343                 for (i = 1; i < arity; ++i) {
6344                         const ir_node *node = get_irn_n(n, i);
6345                         if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6346                                 ins_sorted = false;
6347                                 break;
6348                         }
6349                         last = node;
6350                 }
6351
6352                 if (!ins_sorted) {
6353                         ir_node **ins     = get_irn_in(n)+1;
6354                         ir_node **new_ins = XMALLOCN(ir_node*, arity);
6355                         memcpy(new_ins, ins, arity*sizeof(ins[0]));
6356                         qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6357                         set_irn_in(n, arity, new_ins);
6358                         xfree(new_ins);
6359                 }
6360         }
6361 }
6362
6363 ir_node *identify_remember(ir_node *n)
6364 {
6365         ir_graph *irg         = get_irn_irg(n);
6366         pset     *value_table = irg->value_table;
6367         ir_node  *nn;
6368
6369         if (value_table == NULL)
6370                 return n;
6371
6372         ir_normalize_node(n);
6373         /* lookup or insert in hash table with given hash key. */
6374         nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6375
6376         if (nn != n) {
6377                 /* n is reachable again */
6378                 edges_node_revival(nn);
6379         }
6380
6381         return nn;
6382 }
6383
6384 /**
6385  * During construction we set the op_pin_state_pinned flag in the graph right
6386  * when the optimization is performed.  The flag turning on procedure global
6387  * cse could be changed between two allocations.  This way we are safe.
6388  *
6389  * @param n            The node to lookup
6390  */
6391 static inline ir_node *identify_cons(ir_node *n)
6392 {
6393         ir_node *old = n;
6394
6395         n = identify_remember(n);
6396         if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6397                 ir_graph *irg = get_irn_irg(n);
6398                 set_irg_pinned(irg, op_pin_state_floats);
6399         }
6400         return n;
6401 }
6402
6403 void add_identities(ir_node *node)
6404 {
6405         if (!get_opt_cse())
6406                 return;
6407         if (is_Block(node))
6408                 return;
6409
6410         identify_remember(node);
6411 }
6412
6413 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6414 {
6415         ir_graph *rem = current_ir_graph;
6416
6417         current_ir_graph = irg;
6418         foreach_pset(irg->value_table, ir_node, node) {
6419                 visit(node, env);
6420         }
6421         current_ir_graph = rem;
6422 }
6423
6424 ir_node *optimize_node(ir_node *n)
6425 {
6426         ir_node   *oldn = n;
6427         ir_graph  *irg  = get_irn_irg(n);
6428         unsigned   iro  = get_irn_opcode(n);
6429         ir_tarval *tv;
6430
6431         /* Always optimize Phi nodes: part of the construction. */
6432         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6433
6434         /* constant expression evaluation / constant folding */
6435         if (get_opt_constant_folding()) {
6436                 /* neither constants nor Tuple values can be evaluated */
6437                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6438                         /* try to evaluate */
6439                         tv = computed_value(n);
6440                         if (tv != tarval_bad) {
6441                                 ir_node *nw;
6442                                 size_t node_size;
6443
6444                                 /*
6445                                  * we MUST copy the node here temporarily, because it's still
6446                                  * needed for DBG_OPT_CSTEVAL
6447                                  */
6448                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
6449                                 oldn = (ir_node*)alloca(node_size);
6450
6451                                 memcpy(oldn, n, node_size);
6452                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6453
6454                                 /* ARG, copy the in array, we need it for statistics */
6455                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6456
6457                                 /* note the inplace edges module */
6458                                 edges_node_deleted(n);
6459
6460                                 /* evaluation was successful -- replace the node. */
6461                                 irg_kill_node(irg, n);
6462                                 nw = new_r_Const(irg, tv);
6463
6464                                 DBG_OPT_CSTEVAL(oldn, nw);
6465                                 return nw;
6466                         }
6467                 }
6468         }
6469
6470         /* remove unnecessary nodes */
6471         if (get_opt_algebraic_simplification() ||
6472             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6473             (iro == iro_Id)   ||
6474             (iro == iro_Proj) ||
6475             (iro == iro_Block)  )  /* Flags tested local. */
6476                 n = equivalent_node(n);
6477
6478         /* Common Subexpression Elimination.
6479          *
6480          * Checks whether n is already available.
6481          * The block input is used to distinguish different subexpressions. Right
6482          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6483          * subexpressions within a block.
6484          */
6485         if (get_opt_cse())
6486                 n = identify_cons(n);
6487
6488         if (n != oldn) {
6489                 edges_node_deleted(oldn);
6490
6491                 /* We found an existing, better node, so we can deallocate the old node. */
6492                 irg_kill_node(irg, oldn);
6493                 return n;
6494         }
6495
6496         /* Some more constant expression evaluation that does not allow to
6497            free the node. */
6498         iro = get_irn_opcode(n);
6499         if (get_opt_algebraic_simplification() ||
6500                 (iro == iro_Cond) ||
6501                 (iro == iro_Proj)) {    /* Flags tested local. */
6502                 n = transform_node(n);
6503         }
6504
6505         /* Now we have a legal, useful node. Enter it in hash table for CSE */
6506         if (get_opt_cse()) {
6507                 ir_node *o = n;
6508                 n = identify_remember(o);
6509                 if (o != n)
6510                         DBG_OPT_CSE(o, n);
6511         }
6512
6513         return n;
6514 }
6515
6516 ir_node *optimize_in_place_2(ir_node *n)
6517 {
6518         if (!get_opt_optimize() && !is_Phi(n)) return n;
6519
6520         if (is_Deleted(n))
6521                 return n;
6522
6523         /** common subexpression elimination **/
6524         /* Checks whether n is already available. */
6525         /* The block input is used to distinguish different subexpressions.
6526          * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6527          * only finds common subexpressions within a block. */
6528         if (get_opt_cse()) {
6529                 ir_node *o = n;
6530                 n = identify_remember(n);
6531                 if (n != o) {
6532                         DBG_OPT_CSE(o, n);
6533                         /* we have another existing node now, we do not optimize it here */
6534                         return n;
6535                 }
6536         }
6537
6538         n = transform_node(n);
6539
6540         /* Now we can verify the node, as it has no dead inputs any more. */
6541         irn_verify(n);
6542
6543         /* Now we have a legal, useful node. Enter it in hash table for cse.
6544          *
6545          * Note: This is only necessary because some of the optimisations
6546          * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6547          * bad practice and should be fixed sometime.
6548          */
6549         if (get_opt_cse()) {
6550                 ir_node *o = n;
6551                 n = identify_remember(o);
6552                 if (o != n)
6553                         DBG_OPT_CSE(o, n);
6554         }
6555
6556         return n;
6557 }
6558
6559 ir_node *optimize_in_place(ir_node *n)
6560 {
6561         ir_graph *irg = get_irn_irg(n);
6562
6563         if (get_opt_global_cse())
6564                 set_irg_pinned(irg, op_pin_state_floats);
6565
6566         /* FIXME: Maybe we could also test whether optimizing the node can
6567            change the control graph. */
6568         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6569         return optimize_in_place_2(n);
6570 }