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