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