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