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