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