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