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