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