Normalization puts constants on teh right side of commutative nodes.
[libfirm] / ir / ir / iropt.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #include "irnode_t.h"
35 #include "irgraph_t.h"
36 #include "iredges_t.h"
37 #include "irmode_t.h"
38 #include "iropt_t.h"
39 #include "ircons_t.h"
40 #include "irgmod.h"
41 #include "irvrfy.h"
42 #include "tv_t.h"
43 #include "dbginfo_t.h"
44 #include "iropt_dbg.h"
45 #include "irflag_t.h"
46 #include "irhooks.h"
47 #include "irarch.h"
48 #include "hashptr.h"
49 #include "archop.h"
50 #include "opt_polymorphy.h"
51 #include "opt_confirms.h"
52 #include "irtools.h"
53 #include "xmalloc.h"
54
55 /* Make types visible to allow most efficient access */
56 #include "entity_t.h"
57
58 /**
59  * Return the value of a Constant.
60  */
61 static tarval *computed_value_Const(ir_node *n) {
62         return get_Const_tarval(n);
63 }  /* computed_value_Const */
64
65 /**
66  * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
67  */
68 static tarval *computed_value_SymConst(ir_node *n) {
69         ir_type   *type;
70         ir_entity *ent;
71
72         switch (get_SymConst_kind(n)) {
73         case symconst_type_size:
74                 type = get_SymConst_type(n);
75                 if (get_type_state(type) == layout_fixed)
76                         return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
77                 break;
78         case symconst_type_align:
79                 type = get_SymConst_type(n);
80                 if (get_type_state(type) == layout_fixed)
81                         return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
82                 break;
83         case symconst_ofs_ent:
84                 ent  = get_SymConst_entity(n);
85                 type = get_entity_owner(ent);
86                 if (get_type_state(type) == layout_fixed)
87                         return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
88                 break;
89         default:
90                 break;
91         }
92         return tarval_bad;
93 }  /* computed_value_SymConst */
94
95 /**
96  * Return the value of an Add.
97  */
98 static tarval *computed_value_Add(ir_node *n) {
99         ir_node *a = get_Add_left(n);
100         ir_node *b = get_Add_right(n);
101
102         tarval *ta = value_of(a);
103         tarval *tb = value_of(b);
104
105         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
106                 return tarval_add(ta, tb);
107
108         return tarval_bad;
109 }  /* computed_value_Add */
110
111 /**
112  * Return the value of a Sub.
113  * Special case: a - a
114  */
115 static tarval *computed_value_Sub(ir_node *n) {
116         ir_node *a = get_Sub_left(n);
117         ir_node *b = get_Sub_right(n);
118         tarval *ta;
119         tarval *tb;
120
121         /* a - a */
122         if (a == b && !is_Bad(a))
123                 return get_mode_null(get_irn_mode(n));
124
125         ta = value_of(a);
126         tb = value_of(b);
127
128         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
129                 return tarval_sub(ta, tb);
130
131         return tarval_bad;
132 }  /* computed_value_Sub */
133
134 /**
135  * Return the value of a Carry.
136  * Special : a op 0, 0 op b
137  */
138 static tarval *computed_value_Carry(ir_node *n) {
139         ir_node *a = get_binop_left(n);
140         ir_node *b = get_binop_right(n);
141         ir_mode *m = get_irn_mode(n);
142
143         tarval *ta = value_of(a);
144         tarval *tb = value_of(b);
145
146         if ((ta != tarval_bad) && (tb != tarval_bad)) {
147                 tarval_add(ta, tb);
148                 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
149         } else {
150                 if (   (classify_tarval(ta) == TV_CLASSIFY_NULL)
151                         || (classify_tarval(tb) == TV_CLASSIFY_NULL))
152                         return get_mode_null(m);
153         }
154         return tarval_bad;
155 }  /* computed_value_Carry */
156
157 /**
158  * Return the value of a Borrow.
159  * Special : a op 0
160  */
161 static tarval *computed_value_Borrow(ir_node *n) {
162         ir_node *a = get_binop_left(n);
163         ir_node *b = get_binop_right(n);
164         ir_mode *m = get_irn_mode(n);
165
166         tarval *ta = value_of(a);
167         tarval *tb = value_of(b);
168
169         if ((ta != tarval_bad) && (tb != tarval_bad)) {
170                 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
171         } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
172                 return get_mode_null(m);
173         }
174         return tarval_bad;
175 }  /* computed_value_Borrow */
176
177 /**
178  * Return the value of an unary Minus.
179  */
180 static tarval *computed_value_Minus(ir_node *n) {
181         ir_node *a = get_Minus_op(n);
182         tarval *ta = value_of(a);
183
184         if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
185                 return tarval_neg(ta);
186
187         return tarval_bad;
188 }  /* computed_value_Minus */
189
190 /**
191  * Return the value of a Mul.
192  */
193 static tarval *computed_value_Mul(ir_node *n) {
194         ir_node *a = get_Mul_left(n);
195         ir_node *b = get_Mul_right(n);
196
197         tarval *ta = value_of(a);
198         tarval *tb = value_of(b);
199
200         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
201                 return tarval_mul(ta, tb);
202         } else {
203         /* a*0 = 0 or 0*b = 0:
204            calls computed_value recursive and returns the 0 with proper
205            mode. */
206                 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
207                         return ta;
208                 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
209                         return tb;
210         }
211         return tarval_bad;
212 }  /* computed_value_Mul */
213
214 /**
215  * Return the value of a floating point Quot.
216  */
217 static tarval *computed_value_Quot(ir_node *n) {
218         ir_node *a = get_Quot_left(n);
219         ir_node *b = get_Quot_right(n);
220
221         tarval *ta = value_of(a);
222         tarval *tb = value_of(b);
223
224         /* This was missing in original implementation. Why? */
225         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
226                 if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
227                         return tarval_quo(ta, tb);
228         }
229         return tarval_bad;
230 }  /* computed_value_Quot */
231
232 /**
233  * Calculate the value of an integer Div of two nodes.
234  * Special case: 0 / b
235  */
236 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
237         tarval *ta = value_of(a);
238         tarval *tb = value_of(b);
239
240         /* Compute c1 / c2 or 0 / a, a != 0 */
241         if (ta != tarval_bad) {
242                 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
243                         return tarval_div(ta, tb);
244                 else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
245                         return ta;
246         }
247         return tarval_bad;
248 }  /* do_computed_value_Div */
249
250 /**
251  * Return the value of an integer Div.
252  */
253 static tarval *computed_value_Div(ir_node *n) {
254         return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
255 }  /* computed_value_Div */
256
257 /**
258  * Calculate the value of an integer Mod of two nodes.
259  * Special case: a % 1
260  */
261 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
262         tarval *ta = value_of(a);
263         tarval *tb = value_of(b);
264
265         /* Compute c1 % c2 or a % 1 */
266         if (tb != tarval_bad) {
267                 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
268                         return tarval_mod(ta, tb);
269                 else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
270                         return get_mode_null(get_irn_mode(a));
271         }
272         return tarval_bad;
273 }  /* do_computed_value_Mod */
274
275 /**
276  * Return the value of an integer Mod.
277  */
278 static tarval *computed_value_Mod(ir_node *n) {
279         return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
280 }  /* computed_value_Mod */
281
282 /**
283  * Return the value of an Abs.
284  */
285 static tarval *computed_value_Abs(ir_node *n) {
286         ir_node *a = get_Abs_op(n);
287         tarval *ta = value_of(a);
288
289         if (ta != tarval_bad)
290                 return tarval_abs(ta);
291
292         return tarval_bad;
293 }  /* computed_value_Abs */
294
295 /**
296  * Return the value of an And.
297  * Special case: a & 0, 0 & b
298  */
299 static tarval *computed_value_And(ir_node *n) {
300         ir_node *a = get_And_left(n);
301         ir_node *b = get_And_right(n);
302
303         tarval *ta = value_of(a);
304         tarval *tb = value_of(b);
305
306         if ((ta != tarval_bad) && (tb != tarval_bad)) {
307                 return tarval_and (ta, tb);
308         } else {
309                 tarval *v;
310
311                 if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
312                         || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
313                         return v;
314                 }
315         }
316         return tarval_bad;
317 }  /* computed_value_And */
318
319 /**
320  * Return the value of an Or.
321  * Special case: a | 1...1, 1...1 | b
322  */
323 static tarval *computed_value_Or(ir_node *n) {
324         ir_node *a = get_Or_left(n);
325         ir_node *b = get_Or_right(n);
326
327         tarval *ta = value_of(a);
328         tarval *tb = value_of(b);
329
330         if ((ta != tarval_bad) && (tb != tarval_bad)) {
331                 return tarval_or (ta, tb);
332         } else {
333                 tarval *v;
334                 if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
335                         || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
336                         return v;
337                 }
338         }
339         return tarval_bad;
340 }  /* computed_value_Or */
341
342 /**
343  * Return the value of an Eor.
344  */
345 static tarval *computed_value_Eor(ir_node *n) {
346         ir_node *a = get_Eor_left(n);
347         ir_node *b = get_Eor_right(n);
348
349         tarval *ta, *tb;
350
351         if (a == b)
352                 return get_mode_null(get_irn_mode(n));
353
354         ta = value_of(a);
355         tb = value_of(b);
356
357         if ((ta != tarval_bad) && (tb != tarval_bad)) {
358                 return tarval_eor (ta, tb);
359         }
360         return tarval_bad;
361 }  /* computed_value_Eor */
362
363 /**
364  * Return the value of a Not.
365  */
366 static tarval *computed_value_Not(ir_node *n) {
367         ir_node *a = get_Not_op(n);
368         tarval *ta = value_of(a);
369
370         if (ta != tarval_bad)
371                 return tarval_not(ta);
372
373         return tarval_bad;
374 }  /* computed_value_Not */
375
376 /**
377  * Return the value of a Shl.
378  */
379 static tarval *computed_value_Shl(ir_node *n) {
380         ir_node *a = get_Shl_left(n);
381         ir_node *b = get_Shl_right(n);
382
383         tarval *ta = value_of(a);
384         tarval *tb = value_of(b);
385
386         if ((ta != tarval_bad) && (tb != tarval_bad)) {
387                 return tarval_shl (ta, tb);
388         }
389         return tarval_bad;
390 }  /* computed_value_Shl */
391
392 /**
393  * Return the value of a Shr.
394  */
395 static tarval *computed_value_Shr(ir_node *n) {
396         ir_node *a = get_Shr_left(n);
397         ir_node *b = get_Shr_right(n);
398
399         tarval *ta = value_of(a);
400         tarval *tb = value_of(b);
401
402         if ((ta != tarval_bad) && (tb != tarval_bad)) {
403                 return tarval_shr (ta, tb);
404         }
405         return tarval_bad;
406 }  /* computed_value_Shr */
407
408 /**
409  * Return the value of a Shrs.
410  */
411 static tarval *computed_value_Shrs(ir_node *n) {
412         ir_node *a = get_Shrs_left(n);
413         ir_node *b = get_Shrs_right(n);
414
415         tarval *ta = value_of(a);
416         tarval *tb = value_of(b);
417
418         if ((ta != tarval_bad) && (tb != tarval_bad)) {
419                 return tarval_shrs (ta, tb);
420         }
421         return tarval_bad;
422 }  /* computed_value_Shrs */
423
424 /**
425  * Return the value of a Rot.
426  */
427 static tarval *computed_value_Rot(ir_node *n) {
428         ir_node *a = get_Rot_left(n);
429         ir_node *b = get_Rot_right(n);
430
431         tarval *ta = value_of(a);
432         tarval *tb = value_of(b);
433
434         if ((ta != tarval_bad) && (tb != tarval_bad)) {
435                 return tarval_rot (ta, tb);
436         }
437         return tarval_bad;
438 }  /* computed_value_Rot */
439
440 /**
441  * Return the value of a Conv.
442  */
443 static tarval *computed_value_Conv(ir_node *n) {
444         ir_node *a = get_Conv_op(n);
445         tarval *ta = value_of(a);
446
447         if (ta != tarval_bad)
448                 return tarval_convert_to(ta, get_irn_mode(n));
449
450         return tarval_bad;
451 }  /* computed_value_Conv */
452
453 /**
454  * Return the value of a Proj(Cmp).
455  *
456  * This performs a first step of unreachable code elimination.
457  * Proj can not be computed, but folding a Cmp above the Proj here is
458  * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
459  * only 1 is used.
460  * There are several case where we can evaluate a Cmp node, see later.
461  */
462 static tarval *computed_value_Proj_Cmp(ir_node *n) {
463         ir_node *a   = get_Proj_pred(n);
464         ir_node *aa  = get_Cmp_left(a);
465         ir_node *ab  = get_Cmp_right(a);
466         long proj_nr = get_Proj_proj(n);
467
468         /*
469          * BEWARE: a == a is NOT always True for floating Point values, as
470          * NaN != NaN is defined, so we must check this here.
471          */
472         if (aa == ab && (
473                 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
474                 ) { /* 1.: */
475
476                 /* This is a trick with the bits used for encoding the Cmp
477                    Proj numbers, the following statement is not the same:
478                 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
479                 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
480         }
481         else {
482                 tarval *taa = value_of(aa);
483                 tarval *tab = value_of(ab);
484                 ir_mode *mode = get_irn_mode(aa);
485
486                 /*
487                  * The predecessors of Cmp are target values.  We can evaluate
488                  * the Cmp.
489                  */
490                 if ((taa != tarval_bad) && (tab != tarval_bad)) {
491                         /* strange checks... */
492                         pn_Cmp flags = tarval_cmp(taa, tab);
493                         if (flags != pn_Cmp_False) {
494                                 return new_tarval_from_long (proj_nr & flags, mode_b);
495                         }
496                 }
497                 /* for integer values, we can check against MIN/MAX */
498                 else if (mode_is_int(mode)) {
499                         /* MIN <=/> x.  This results in true/false. */
500                         if (taa == get_mode_min(mode)) {
501                                 /* a compare with the MIN value */
502                                 if (proj_nr == pn_Cmp_Le)
503                                         return get_tarval_b_true();
504                                 else if (proj_nr == pn_Cmp_Gt)
505                                         return get_tarval_b_false();
506                         }
507                         /* x >=/< MIN.  This results in true/false. */
508                         else
509                                 if (tab == get_mode_min(mode)) {
510                                         /* a compare with the MIN value */
511                                         if (proj_nr == pn_Cmp_Ge)
512                                                 return get_tarval_b_true();
513                                         else if (proj_nr == pn_Cmp_Lt)
514                                                 return get_tarval_b_false();
515                                 }
516                                 /* MAX >=/< x.  This results in true/false. */
517                                 else if (taa == get_mode_max(mode)) {
518                                         if (proj_nr == pn_Cmp_Ge)
519                                                 return get_tarval_b_true();
520                                         else if (proj_nr == pn_Cmp_Lt)
521                                                 return get_tarval_b_false();
522                                 }
523                                 /* x <=/> MAX.  This results in true/false. */
524                                 else if (tab == get_mode_max(mode)) {
525                                         if (proj_nr == pn_Cmp_Le)
526                                                 return get_tarval_b_true();
527                                         else if (proj_nr == pn_Cmp_Gt)
528                                                 return get_tarval_b_false();
529                                 }
530                 }
531                 /*
532                  * The predecessors are Allocs or (void*)(0) constants.  Allocs never
533                  * return NULL, they raise an exception.   Therefore we can predict
534                  * the Cmp result.
535                  */
536                 else {
537                         ir_node *aaa = skip_Id(skip_Proj(aa));
538                         ir_node *aba = skip_Id(skip_Proj(ab));
539
540                         if (   (   (/* aa is ProjP and aaa is Alloc */
541                                        (get_irn_op(aa) == op_Proj)
542                                     && (mode_is_reference(get_irn_mode(aa)))
543                                     && (get_irn_op(aaa) == op_Alloc))
544                                 && (   (/* ab is NULL */
545                                            (get_irn_op(ab) == op_Const)
546                                         && (mode_is_reference(get_irn_mode(ab)))
547                                         && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
548                                     || (/* ab is other Alloc */
549                                            (get_irn_op(ab) == op_Proj)
550                                         && (mode_is_reference(get_irn_mode(ab)))
551                                         && (get_irn_op(aba) == op_Alloc)
552                                         && (aaa != aba))))
553                             || (/* aa is NULL and aba is Alloc */
554                                    (get_irn_op(aa) == op_Const)
555                                 && (mode_is_reference(get_irn_mode(aa)))
556                                 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
557                                 && (get_irn_op(ab) == op_Proj)
558                                 && (mode_is_reference(get_irn_mode(ab)))
559                                 && (get_irn_op(aba) == op_Alloc)))
560                                 /* 3.: */
561                         return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
562                 }
563         }
564         return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
565 }  /* computed_value_Proj_Cmp */
566
567 /**
568  * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
569  * Proj(DivMod) and Proj(Quot).
570  */
571 static tarval *computed_value_Proj(ir_node *n) {
572         ir_node *a = get_Proj_pred(n);
573         long proj_nr;
574
575         switch (get_irn_opcode(a)) {
576         case iro_Cmp:
577                 return computed_value_Proj_Cmp(n);
578
579         case iro_DivMod:
580                 /* compute either the Div or the Mod part */
581                 proj_nr = get_Proj_proj(n);
582                 if (proj_nr == pn_DivMod_res_div)
583                         return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
584                 else if (proj_nr == pn_DivMod_res_mod)
585                         return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
586                 break;
587
588         case iro_Div:
589                 if (get_Proj_proj(n) == pn_Div_res)
590                         return computed_value(a);
591                 break;
592
593         case iro_Mod:
594                 if (get_Proj_proj(n) == pn_Mod_res)
595                         return computed_value(a);
596                 break;
597
598         case iro_Quot:
599                 if (get_Proj_proj(n) == pn_Quot_res)
600                         return computed_value(a);
601                 break;
602
603         default:
604                 return tarval_bad;
605         }
606         return tarval_bad;
607 }  /* computed_value_Proj */
608
609 /**
610  * Calculate the value of a Mux: can be evaluated, if the
611  * sel and the right input are known.
612  */
613 static tarval *computed_value_Mux(ir_node *n) {
614         ir_node *sel = get_Mux_sel(n);
615         tarval *ts = value_of(sel);
616
617         if (ts == get_tarval_b_true()) {
618                 ir_node *v = get_Mux_true(n);
619                 return value_of(v);
620         }
621         else if (ts == get_tarval_b_false()) {
622                 ir_node *v = get_Mux_false(n);
623                 return value_of(v);
624         }
625         return tarval_bad;
626 }  /* computed_value_Mux */
627
628 /**
629  * Calculate the value of a Psi: can be evaluated, if a condition is true
630  * and all previous conditions are false. If all conditions are false
631  * we evaluate to the default one.
632  */
633 static tarval *computed_value_Psi(ir_node *n) {
634         if (is_Mux(n))
635                 return computed_value_Mux(n);
636         return tarval_bad;
637 }  /* computed_value_Psi */
638
639 /**
640  * Calculate the value of a Confirm: can be evaluated,
641  * if it has the form Confirm(x, '=', Const).
642  */
643 static tarval *computed_value_Confirm(ir_node *n) {
644         return get_Confirm_cmp(n) == pn_Cmp_Eq ?
645                 value_of(get_Confirm_bound(n)) : tarval_bad;
646 }  /* computed_value_Confirm */
647
648 /**
649  * If the parameter n can be computed, return its value, else tarval_bad.
650  * Performs constant folding.
651  *
652  * @param n  The node this should be evaluated
653  */
654 tarval *computed_value(ir_node *n) {
655         if (n->op->ops.computed_value)
656                 return n->op->ops.computed_value(n);
657         return tarval_bad;
658 }  /* computed_value */
659
660 /**
661  * Set the default computed_value evaluator in an ir_op_ops.
662  *
663  * @param code   the opcode for the default operation
664  * @param ops    the operations initialized
665  *
666  * @return
667  *    The operations.
668  */
669 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
670 {
671 #define CASE(a)                                    \
672         case iro_##a:                                  \
673                 ops->computed_value  = computed_value_##a; \
674                 break
675
676         switch (code) {
677         CASE(Const);
678         CASE(SymConst);
679         CASE(Add);
680         CASE(Sub);
681         CASE(Minus);
682         CASE(Mul);
683         CASE(Quot);
684         CASE(Div);
685         CASE(Mod);
686         CASE(Abs);
687         CASE(And);
688         CASE(Or);
689         CASE(Eor);
690         CASE(Not);
691         CASE(Shl);
692         CASE(Shr);
693         CASE(Shrs);
694         CASE(Rot);
695         CASE(Carry);
696         CASE(Borrow);
697         CASE(Conv);
698         CASE(Proj);
699         CASE(Mux);
700         CASE(Psi);
701         CASE(Confirm);
702         default:
703                 /* leave NULL */;
704         }
705
706         return ops;
707 #undef CASE
708 }  /* firm_set_default_computed_value */
709
710 /**
711  * Returns a equivalent block for another block.
712  * If the block has only one predecessor, this is
713  * the equivalent one. If the only predecessor of a block is
714  * the block itself, this is a dead block.
715  *
716  * If both predecessors of a block are the branches of a binary
717  * Cond, the equivalent block is Cond's block.
718  *
719  * If all predecessors of a block are bad or lies in a dead
720  * block, the current block is dead as well.
721  *
722  * Note, that blocks are NEVER turned into Bad's, instead
723  * the dead_block flag is set. So, never test for is_Bad(block),
724  * always use is_dead_Block(block).
725  */
726 static ir_node *equivalent_node_Block(ir_node *n)
727 {
728         ir_node *oldn = n;
729         int n_preds   = get_Block_n_cfgpreds(n);
730
731         /* The Block constructor does not call optimize, but mature_immBlock
732         calls the optimization. */
733         assert(get_Block_matured(n));
734
735         /* Straightening: a single entry Block following a single exit Block
736            can be merged, if it is not the Start block. */
737         /* !!! Beware, all Phi-nodes of n must have been optimized away.
738            This should be true, as the block is matured before optimize is called.
739            But what about Phi-cycles with the Phi0/Id that could not be resolved?
740            Remaining Phi nodes are just Ids. */
741         if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
742                 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
743                 if (predblock == oldn) {
744                         /* Jmp jumps into the block it is in -- deal self cycle. */
745                         n = set_Block_dead(n);
746                         DBG_OPT_DEAD_BLOCK(oldn, n);
747                 } else if (get_opt_control_flow_straightening()) {
748                         n = predblock;
749                         DBG_OPT_STG(oldn, n);
750                 }
751         } else if ((n_preds == 1) &&
752                    (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
753                 ir_node *predblock = get_Block_cfgpred_block(n, 0);
754                 if (predblock == oldn) {
755                         /* Jmp jumps into the block it is in -- deal self cycle. */
756                         n = set_Block_dead(n);
757                         DBG_OPT_DEAD_BLOCK(oldn, n);
758                 }
759         } else if ((n_preds == 2) &&
760                    (get_opt_control_flow_weak_simplification())) {
761                 /* Test whether Cond jumps twice to this block
762                  * The more general case which more than 2 predecessors is handles
763                  * in optimize_cf(), we handle only this special case for speed here.
764                  */
765                 ir_node *a = get_Block_cfgpred(n, 0);
766                 ir_node *b = get_Block_cfgpred(n, 1);
767
768                 if ((get_irn_op(a) == op_Proj) &&
769                     (get_irn_op(b) == op_Proj) &&
770                     (get_Proj_pred(a) == get_Proj_pred(b)) &&
771                     (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
772                     (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
773                         /* Also a single entry Block following a single exit Block.  Phis have
774                            twice the same operand and will be optimized away. */
775                         n = get_nodes_block(get_Proj_pred(a));
776                         DBG_OPT_IFSIM1(oldn, a, b, n);
777                 }
778         } else if (get_opt_unreachable_code() &&
779                    (n != get_irg_start_block(current_ir_graph)) &&
780                    (n != get_irg_end_block(current_ir_graph))    ) {
781                 int i;
782
783                 /* If all inputs are dead, this block is dead too, except if it is
784                    the start or end block.  This is one step of unreachable code
785                    elimination */
786                 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
787                         ir_node *pred = get_Block_cfgpred(n, i);
788                         ir_node *pred_blk;
789
790                         if (is_Bad(pred)) continue;
791                         pred_blk = get_nodes_block(skip_Proj(pred));
792
793                         if (is_Block_dead(pred_blk)) continue;
794
795                         if (pred_blk != n) {
796                                 /* really found a living input */
797                                 break;
798                         }
799                 }
800                 if (i < 0) {
801                         n = set_Block_dead(n);
802                         DBG_OPT_DEAD_BLOCK(oldn, n);
803                 }
804         }
805
806         return n;
807 }  /* equivalent_node_Block */
808
809 /**
810  * Returns a equivalent node for a Jmp, a Bad :-)
811  * Of course this only happens if the Block of the Jmp is dead.
812  */
813 static ir_node *equivalent_node_Jmp(ir_node *n) {
814         /* unreachable code elimination */
815         if (is_Block_dead(get_nodes_block(n)))
816                 n = new_Bad();
817
818         return n;
819 }  /* equivalent_node_Jmp */
820
821 /** Raise is handled in the same way as Jmp. */
822 #define equivalent_node_Raise   equivalent_node_Jmp
823
824
825 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
826    See transform_node_Proj_Cond(). */
827
828 /**
829  * Optimize operations that are commutative and have neutral 0,
830  * so a op 0 = 0 op a = a.
831  */
832 static ir_node *equivalent_node_neutral_zero(ir_node *n)
833 {
834         ir_node *oldn = n;
835
836         ir_node *a = get_binop_left(n);
837         ir_node *b = get_binop_right(n);
838
839         tarval *tv;
840         ir_node *on;
841
842         /* After running compute_node there is only one constant predecessor.
843            Find this predecessors value and remember the other node: */
844         if ((tv = value_of(a)) != tarval_bad) {
845                 on = b;
846         } else if ((tv = value_of(b)) != tarval_bad) {
847                 on = a;
848         } else
849                 return n;
850
851         /* If this predecessors constant value is zero, the operation is
852          * unnecessary. Remove it.
853          *
854          * Beware: If n is a Add, the mode of on and n might be different
855          * which happens in this rare construction: NULL + 3.
856          * Then, a Conv would be needed which we cannot include here.
857          */
858         if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
859                 if (get_irn_mode(on) == get_irn_mode(n)) {
860                         n = on;
861
862                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
863                 }
864         }
865
866         return n;
867 }  /* equivalent_node_neutral_zero */
868
869 /**
870  * Eor is commutative and has neutral 0.
871  */
872 #define equivalent_node_Eor  equivalent_node_neutral_zero
873
874 /*
875  * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
876  *
877  * The second one looks strange, but this construct
878  * is used heavily in the LCC sources :-).
879  *
880  * Beware: The Mode of an Add may be different than the mode of its
881  * predecessors, so we could not return a predecessors in all cases.
882  */
883 static ir_node *equivalent_node_Add(ir_node *n) {
884         ir_node *oldn = n;
885         ir_node *left, *right;
886         ir_mode *mode = get_irn_mode(n);
887
888         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
889         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
890                 return n;
891
892         n = equivalent_node_neutral_zero(n);
893         if (n != oldn)
894                 return n;
895
896         left  = get_Add_left(n);
897         right = get_Add_right(n);
898
899         if (get_irn_op(left) == op_Sub) {
900                 if (get_Sub_right(left) == right) {
901                         /* (a - x) + x */
902
903                         n = get_Sub_left(left);
904                         if (mode == get_irn_mode(n)) {
905                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
906                                 return n;
907                         }
908                 }
909         }
910         if (get_irn_op(right) == op_Sub) {
911                 if (get_Sub_right(right) == left) {
912                         /* x + (a - x) */
913
914                         n = get_Sub_left(right);
915                         if (mode == get_irn_mode(n)) {
916                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
917                                 return n;
918                         }
919                 }
920         }
921         return n;
922 }  /* equivalent_node_Add */
923
924 /**
925  * optimize operations that are not commutative but have neutral 0 on left,
926  * so a op 0 = a.
927  */
928 static ir_node *equivalent_node_left_zero(ir_node *n) {
929         ir_node *oldn = n;
930
931         ir_node *a = get_binop_left(n);
932         ir_node *b = get_binop_right(n);
933
934         if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
935                 n = a;
936
937                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
938         }
939         return n;
940 }  /* equivalent_node_left_zero */
941
942 #define equivalent_node_Shl   equivalent_node_left_zero
943 #define equivalent_node_Shr   equivalent_node_left_zero
944 #define equivalent_node_Shrs  equivalent_node_left_zero
945 #define equivalent_node_Rot   equivalent_node_left_zero
946
947 /**
948  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
949  *
950  * The second one looks strange, but this construct
951  * is used heavily in the LCC sources :-).
952  *
953  * Beware: The Mode of a Sub may be different than the mode of its
954  * predecessors, so we could not return a predecessors in all cases.
955  */
956 static ir_node *equivalent_node_Sub(ir_node *n) {
957         ir_node *oldn = n;
958         ir_node *a, *b;
959         ir_mode *mode = get_irn_mode(n);
960
961         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
962         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
963                 return n;
964
965         a = get_Sub_left(n);
966         b = get_Sub_right(n);
967
968         /* Beware: modes might be different */
969         if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
970                 if (mode == get_irn_mode(a)) {
971                         n = a;
972
973                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
974                 }
975         } else if (get_irn_op(a) == op_Add) {
976                 if (mode_wrap_around(mode)) {
977                         ir_node *left  = get_Add_left(a);
978                         ir_node *right = get_Add_right(a);
979
980                         if (left == b) {
981                                 if (mode == get_irn_mode(right)) {
982                                         n = right;
983                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
984                                 }
985                         } else if (right == b) {
986                                 if (mode == get_irn_mode(left)) {
987                                         n = left;
988                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
989                                 }
990                         }
991                 }
992         }
993         return n;
994 }  /* equivalent_node_Sub */
995
996
997 /**
998  * Optimize an "idempotent unary op", ie op(op(n)) = n.
999  *
1000  * @todo
1001  *   -(-a) == a, but might overflow two times.
1002  *   We handle it anyway here but the better way would be a
1003  *   flag. This would be needed for Pascal for instance.
1004  */
1005 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1006         ir_node *oldn = n;
1007         ir_node *pred = get_unop_op(n);
1008
1009         /* optimize symmetric unop */
1010         if (get_irn_op(pred) == get_irn_op(n)) {
1011                 n = get_unop_op(pred);
1012                 DBG_OPT_ALGSIM2(oldn, pred, n);
1013         }
1014         return n;
1015 }  /* equivalent_node_idempotent_unop */
1016
1017 /** Optimize Not(Not(x)) == x. */
1018 #define equivalent_node_Not    equivalent_node_idempotent_unop
1019
1020 /** --x == x       ??? Is this possible or can --x raise an
1021                        out of bounds exception if min =! max? */
1022 #define equivalent_node_Minus  equivalent_node_idempotent_unop
1023
1024 /**
1025  * Optimize a * 1 = 1 * a = a.
1026  */
1027 static ir_node *equivalent_node_Mul(ir_node *n) {
1028         ir_node *oldn = n;
1029         ir_node *a = get_Mul_left(n);
1030         ir_node *b = get_Mul_right(n);
1031
1032         /* Mul is commutative and has again an other neutral element. */
1033         if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1034                 n = b;
1035                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1036         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1037                 n = a;
1038                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1039         }
1040         return n;
1041 }  /* equivalent_node_Mul */
1042
1043 /**
1044  * Optimize a / 1 = a.
1045  */
1046 static ir_node *equivalent_node_Div(ir_node *n) {
1047         ir_node *a = get_Div_left(n);
1048         ir_node *b = get_Div_right(n);
1049
1050         /* Div is not commutative. */
1051         if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1052                 /* Turn Div into a tuple (mem, bad, a) */
1053                 ir_node *mem = get_Div_mem(n);
1054                 turn_into_tuple(n, pn_Div_max);
1055                 set_Tuple_pred(n, pn_Div_M,        mem);
1056                 set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
1057                 set_Tuple_pred(n, pn_Div_res,      a);
1058         }
1059         return n;
1060 }  /* equivalent_node_Div */
1061
1062 /**
1063  * Optimize a / 1.0 = a.
1064  */
1065 static ir_node *equivalent_node_Quot(ir_node *n) {
1066         ir_node *a = get_Quot_left(n);
1067         ir_node *b = get_Quot_right(n);
1068
1069         /* Div is not commutative. */
1070         if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1071                 /* Turn Quot into a tuple (mem, bad, a) */
1072                 ir_node *mem = get_Quot_mem(n);
1073                 turn_into_tuple(n, pn_Quot_max);
1074                 set_Tuple_pred(n, pn_Quot_M,        mem);
1075                 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());        /* no exception */
1076                 set_Tuple_pred(n, pn_Quot_res,      a);
1077         }
1078         return n;
1079 }  /* equivalent_node_Quot */
1080
1081 /**
1082  * Optimize a / 1 = a.
1083  */
1084 static ir_node *equivalent_node_DivMod(ir_node *n) {
1085         ir_node *a = get_DivMod_left(n);
1086         ir_node *b = get_DivMod_right(n);
1087
1088         /* Div is not commutative. */
1089         if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1090                 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1091                 ir_node *mem = get_Div_mem(n);
1092                 ir_mode *mode = get_irn_mode(b);
1093
1094                 turn_into_tuple(n, pn_DivMod_max);
1095                 set_Tuple_pred(n, pn_DivMod_M,        mem);
1096                 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
1097                 set_Tuple_pred(n, pn_DivMod_res_div,  a);
1098                 set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
1099         }
1100         return n;
1101 }  /* equivalent_node_DivMod */
1102
1103 /**
1104  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1105  */
1106 static ir_node *equivalent_node_Or(ir_node *n) {
1107         ir_node *oldn = n;
1108
1109         ir_node *a = get_Or_left(n);
1110         ir_node *b = get_Or_right(n);
1111
1112         if (a == b) {
1113                 n = a;    /* Or has it's own neutral element */
1114                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1115         } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1116                 n = b;
1117                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1118         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1119                 n = a;
1120                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1121         }
1122
1123         return n;
1124 }  /* equivalent_node_Or */
1125
1126 /**
1127  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
1128  */
1129 static ir_node *equivalent_node_And(ir_node *n) {
1130         ir_node *oldn = n;
1131
1132         ir_node *a = get_And_left(n);
1133         ir_node *b = get_And_right(n);
1134
1135         if (a == b) {
1136                 n = a;    /* And has it's own neutral element */
1137                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1138         } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1139                 n = b;
1140                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1141         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1142                 n = a;
1143                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1144         }
1145         return n;
1146 }  /* equivalent_node_And */
1147
1148 /**
1149  * Try to remove useless Conv's:
1150  */
1151 static ir_node *equivalent_node_Conv(ir_node *n) {
1152         ir_node *oldn = n;
1153         ir_node *a = get_Conv_op(n);
1154         ir_node *b;
1155
1156         ir_mode *n_mode = get_irn_mode(n);
1157         ir_mode *a_mode = get_irn_mode(a);
1158
1159         if (n_mode == a_mode) { /* No Conv necessary */
1160                 /* leave strict floating point Conv's */
1161                 if (get_Conv_strict(n))
1162                         return n;
1163                 n = a;
1164                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1165         } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1166                 ir_mode *b_mode;
1167
1168                 b = get_Conv_op(a);
1169                 n_mode = get_irn_mode(n);
1170                 b_mode = get_irn_mode(b);
1171
1172                 if (n_mode == b_mode) {
1173                         if (n_mode == mode_b) {
1174                                 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1175                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1176                         } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1177                                 if (smaller_mode(b_mode, a_mode)){
1178                                         n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1179                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1180                                 }
1181                         }
1182                 }
1183         }
1184         return n;
1185 }  /* equivalent_node_Conv */
1186
1187 /**
1188  * A Cast may be removed if the type of the previous node
1189  * is already the type of the Cast.
1190  */
1191 static ir_node *equivalent_node_Cast(ir_node *n) {
1192         ir_node *oldn = n;
1193         ir_node *pred = get_Cast_op(n);
1194
1195         if (get_irn_type(pred) == get_Cast_type(n)) {
1196                 n = pred;
1197                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1198         }
1199         return n;
1200 }  /* equivalent_node_Cast */
1201
1202 /**
1203  * Several optimizations:
1204  * - no Phi in start block.
1205  * - remove Id operators that are inputs to Phi
1206  * - fold Phi-nodes, iff they have only one predecessor except
1207  *   themselves.
1208  */
1209 static ir_node *equivalent_node_Phi(ir_node *n) {
1210         int i, n_preds;
1211
1212         ir_node *oldn = n;
1213         ir_node *block = NULL;     /* to shutup gcc */
1214         ir_node *first_val = NULL; /* to shutup gcc */
1215
1216         if (!get_opt_normalize()) return n;
1217
1218         n_preds = get_Phi_n_preds(n);
1219
1220         block = get_nodes_block(n);
1221         /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1222         assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1223         if ((is_Block_dead(block)) ||                  /* Control dead */
1224                 (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1225                 return new_Bad();                                    /* in the Start Block. */
1226
1227         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1228
1229         /* If the Block has a Bad pred, we also have one. */
1230         for (i = 0;  i < n_preds;  ++i)
1231                 if (is_Bad(get_Block_cfgpred(block, i)))
1232                         set_Phi_pred(n, i, new_Bad());
1233
1234         /* Find first non-self-referencing input */
1235         for (i = 0; i < n_preds; ++i) {
1236                 first_val = get_Phi_pred(n, i);
1237                 if (   (first_val != n)                            /* not self pointer */
1238 #if 1
1239                     && (! is_Bad(first_val))
1240 #endif
1241                    ) {        /* value not dead */
1242                         break;          /* then found first value. */
1243                 }
1244         }
1245
1246         if (i >= n_preds) {
1247                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1248                 return new_Bad();
1249         }
1250
1251         /* search for rest of inputs, determine if any of these
1252         are non-self-referencing */
1253         while (++i < n_preds) {
1254                 ir_node *scnd_val = get_Phi_pred(n, i);
1255                 if (   (scnd_val != n)
1256                     && (scnd_val != first_val)
1257 #if 1
1258                     && (! is_Bad(scnd_val))
1259 #endif
1260                         ) {
1261                         break;
1262                 }
1263         }
1264
1265         if (i >= n_preds) {
1266                 /* Fold, if no multiple distinct non-self-referencing inputs */
1267                 n = first_val;
1268                 DBG_OPT_PHI(oldn, n);
1269         }
1270         return n;
1271 }  /* equivalent_node_Phi */
1272
1273 /**
1274  * Several optimizations:
1275  * - no Sync in start block.
1276  * - fold Sync-nodes, iff they have only one predecessor except
1277  *   themselves.
1278  */
1279 static ir_node *equivalent_node_Sync(ir_node *n) {
1280         int i, n_preds;
1281
1282         ir_node *oldn = n;
1283         ir_node *first_val = NULL; /* to shutup gcc */
1284
1285         if (!get_opt_normalize()) return n;
1286
1287         n_preds = get_Sync_n_preds(n);
1288
1289         /* Find first non-self-referencing input */
1290         for (i = 0; i < n_preds; ++i) {
1291                 first_val = get_Sync_pred(n, i);
1292                 if ((first_val != n)  /* not self pointer */ &&
1293                     (! is_Bad(first_val))
1294                    ) {                /* value not dead */
1295                         break;            /* then found first value. */
1296                 }
1297         }
1298
1299         if (i >= n_preds)
1300                 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1301                 return new_Bad();
1302
1303         /* search the rest of inputs, determine if any of these
1304            are non-self-referencing */
1305         while (++i < n_preds) {
1306                 ir_node *scnd_val = get_Sync_pred(n, i);
1307                 if ((scnd_val != n) &&
1308                     (scnd_val != first_val) &&
1309                     (! is_Bad(scnd_val))
1310                    )
1311                         break;
1312         }
1313
1314         if (i >= n_preds) {
1315                 /* Fold, if no multiple distinct non-self-referencing inputs */
1316                 n = first_val;
1317                 DBG_OPT_SYNC(oldn, n);
1318         }
1319         return n;
1320 }  /* equivalent_node_Sync */
1321
1322 /**
1323  * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324  * ProjX(Load) and ProjX(Store).
1325  */
1326 static ir_node *equivalent_node_Proj(ir_node *n) {
1327         ir_node *oldn = n;
1328         ir_node *a = get_Proj_pred(n);
1329
1330         if ( get_irn_op(a) == op_Tuple) {
1331                 /* Remove the Tuple/Proj combination. */
1332                 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1333                         n = get_Tuple_pred(a, get_Proj_proj(n));
1334                         DBG_OPT_TUPLE(oldn, a, n);
1335                 } else {
1336                         assert(0); /* This should not happen! */
1337                         n = new_Bad();
1338                 }
1339         } else if (get_irn_mode(n) == mode_X) {
1340                 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1341                         /* Remove dead control flow -- early gigo(). */
1342                         n = new_Bad();
1343                 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1344                         ir_op *op = get_irn_op(a);
1345
1346                         if (op == op_Load || op == op_Store) {
1347                                 /* get the load/store address */
1348                                 ir_node *addr = get_irn_n(a, 1);
1349                                 ir_node *confirm;
1350
1351                                 if (value_not_null(addr, &confirm)) {
1352                                         if (confirm == NULL) {
1353                                                 /* this node may float if it did not depend on a Confirm */
1354                                                 set_irn_pinned(a, op_pin_state_floats);
1355                                         }
1356                                         DBG_OPT_EXC_REM(n);
1357                                         return new_Bad();
1358                                 }
1359                         }
1360                 }
1361         }
1362
1363         return n;
1364 }  /* equivalent_node_Proj */
1365
1366 /**
1367  * Remove Id's.
1368  */
1369 static ir_node *equivalent_node_Id(ir_node *n) {
1370         ir_node *oldn = n;
1371
1372         do {
1373                 n = get_Id_pred(n);
1374         } while (get_irn_op(n) == op_Id);
1375
1376         DBG_OPT_ID(oldn, n);
1377         return n;
1378 }  /* equivalent_node_Id */
1379
1380 /**
1381  * Optimize a Mux.
1382  */
1383 static ir_node *equivalent_node_Mux(ir_node *n)
1384 {
1385         ir_node *oldn = n, *sel = get_Mux_sel(n);
1386         tarval *ts = value_of(sel);
1387
1388         /* Mux(true, f, t) == t */
1389         if (ts == tarval_b_true) {
1390                 n = get_Mux_true(n);
1391                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1392         }
1393         /* Mux(false, f, t) == f */
1394         else if (ts == tarval_b_false) {
1395                 n = get_Mux_false(n);
1396                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1397         }
1398         /* Mux(v, x, x) == x */
1399         else if (get_Mux_false(n) == get_Mux_true(n)) {
1400                 n = get_Mux_true(n);
1401                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1402         }
1403         else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1404                 ir_node *cmp = get_Proj_pred(sel);
1405                 long proj_nr = get_Proj_proj(sel);
1406                 ir_node *b   = get_Mux_false(n);
1407                 ir_node *a   = get_Mux_true(n);
1408
1409                 /*
1410                  * Note: normalization puts the constant on the right site,
1411                  * so we check only one case.
1412                  *
1413                  * Note further that these optimization work even for floating point
1414                  * with NaN's because -NaN == NaN.
1415                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1416                  */
1417                 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1418                         if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1419                                 /* Mux(a CMP 0, X, a) */
1420                                 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1421                                         /* Mux(a CMP 0, -a, a) */
1422                                         if (proj_nr == pn_Cmp_Eq) {
1423                                                 /* Mux(a == 0, -a, a)  ==>  -a */
1424                                                 n = b;
1425                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1426                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1427                                                 /* Mux(a != 0, -a, a)  ==> a */
1428                                                 n = a;
1429                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1430                                         }
1431                                 } else if (classify_Const(b) == CNST_NULL) {
1432                                         /* Mux(a CMP 0, 0, a) */
1433                                         if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1434                                                 /* Mux(a != 0, 0, a) ==> a */
1435                                                 n = a;
1436                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1437                                         } else if (proj_nr == pn_Cmp_Eq) {
1438                                                 /* Mux(a == 0, 0, a) ==> 0 */
1439                                                 n = b;
1440                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1441                                         }
1442                                 }
1443                         }
1444                 }
1445         }
1446         return n;
1447 }  /* equivalent_node_Mux */
1448
1449 /**
1450  * Returns a equivalent node of a Psi: if a condition is true
1451  * and all previous conditions are false we know its value.
1452  * If all conditions are false its value is the default one.
1453  */
1454 static ir_node *equivalent_node_Psi(ir_node *n) {
1455         if (is_Mux(n))
1456                 return equivalent_node_Mux(n);
1457         return n;
1458 }  /* equivalent_node_Psi */
1459
1460 /**
1461  * Optimize -a CMP -b into b CMP a.
1462  * This works only for for modes where unary Minus
1463  * cannot Overflow.
1464  * Note that two-complement integers can Overflow
1465  * so it will NOT work.
1466  *
1467  * For == and != can be handled in Proj(Cmp)
1468  */
1469 static ir_node *equivalent_node_Cmp(ir_node *n) {
1470         ir_node *left  = get_Cmp_left(n);
1471         ir_node *right = get_Cmp_right(n);
1472
1473         if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1474                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1475                 left  = get_Minus_op(left);
1476                 right = get_Minus_op(right);
1477                 set_Cmp_left(n, right);
1478                 set_Cmp_right(n, left);
1479         }
1480         return n;
1481 }  /* equivalent_node_Cmp */
1482
1483 /**
1484  * Remove Confirm nodes if setting is on.
1485  * Replace Confirms(x, '=', Constlike) by Constlike.
1486  */
1487 static ir_node *equivalent_node_Confirm(ir_node *n) {
1488         ir_node *pred = get_Confirm_value(n);
1489         pn_Cmp  pnc   = get_Confirm_cmp(n);
1490
1491         if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1492                 /*
1493                  * rare case: two identical Confirms one after another,
1494                  * replace the second one with the first.
1495                  */
1496                 n = pred;
1497         }
1498         if (pnc == pn_Cmp_Eq) {
1499                 ir_node *bound = get_Confirm_bound(n);
1500
1501                 /*
1502                  * Optimize a rare case:
1503                  * Confirm(x, '=', Constlike) ==> Constlike
1504                  */
1505                 if (is_irn_constlike(bound)) {
1506                         DBG_OPT_CONFIRM(n, bound);
1507                         return bound;
1508                 }
1509         }
1510         return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1511 }
1512
1513 /**
1514  * Optimize CopyB(mem, x, x) into a Nop.
1515  */
1516 static ir_node *equivalent_node_CopyB(ir_node *n) {
1517         ir_node *a = get_CopyB_dst(n);
1518         ir_node *b = get_CopyB_src(n);
1519
1520         if (a == b) {
1521                 /* Turn CopyB into a tuple (mem, bad, bad) */
1522                 ir_node *mem = get_CopyB_mem(n);
1523                 turn_into_tuple(n, pn_CopyB_max);
1524                 set_Tuple_pred(n, pn_CopyB_M,        mem);
1525                 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad());        /* no exception */
1526                 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1527         }
1528         return n;
1529 }  /* equivalent_node_CopyB */
1530
1531 /**
1532  * Optimize Bounds(idx, idx, upper) into idx.
1533  */
1534 static ir_node *equivalent_node_Bound(ir_node *n) {
1535         ir_node *idx   = get_Bound_index(n);
1536         ir_node *lower = get_Bound_lower(n);
1537         int ret_tuple = 0;
1538
1539         /* By definition lower < upper, so if idx == lower -->
1540         lower <= idx && idx < upper */
1541         if (idx == lower) {
1542                 /* Turn Bound into a tuple (mem, bad, idx) */
1543                 ret_tuple = 1;
1544         } else {
1545                 ir_node *pred = skip_Proj(idx);
1546
1547                 if (get_irn_op(pred) == op_Bound) {
1548                         /*
1549                          * idx was Bounds_check previously, it is still valid if
1550                          * lower <= pred_lower && pred_upper <= upper.
1551                          */
1552                         ir_node *upper = get_Bound_upper(n);
1553                         if (get_Bound_lower(pred) == lower &&
1554                                 get_Bound_upper(pred) == upper) {
1555                                 /*
1556                                  * One could expect that we simply return the previous
1557                                  * Bound here. However, this would be wrong, as we could
1558                                  * add an exception Proj to a new location than.
1559                                  * So, we must turn in into a tuple
1560                                  */
1561                                 ret_tuple = 1;
1562                         }
1563                 }
1564         }
1565         if (ret_tuple) {
1566                 /* Turn Bound into a tuple (mem, bad, idx) */
1567                 ir_node *mem = get_Bound_mem(n);
1568                 turn_into_tuple(n, pn_Bound_max);
1569                 set_Tuple_pred(n, pn_Bound_M,        mem);
1570                 set_Tuple_pred(n, pn_Bound_X_except, new_Bad());       /* no exception */
1571                 set_Tuple_pred(n, pn_Bound_res,      idx);
1572         }
1573         return n;
1574 }  /* equivalent_node_Bound */
1575
1576 /**
1577  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1578  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1579  * new nodes.  It is therefore safe to free n if the node returned is not n.
1580  * If a node returns a Tuple we can not just skip it.  If the size of the
1581  * in array fits, we transform n into a tuple (e.g., Div).
1582  */
1583 ir_node *equivalent_node(ir_node *n) {
1584         if (n->op->ops.equivalent_node)
1585                 return n->op->ops.equivalent_node(n);
1586         return n;
1587 }  /* equivalent_node */
1588
1589 /**
1590  * Sets the default equivalent node operation for an ir_op_ops.
1591  *
1592  * @param code   the opcode for the default operation
1593  * @param ops    the operations initialized
1594  *
1595  * @return
1596  *    The operations.
1597  */
1598 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1599 {
1600 #define CASE(a)                                      \
1601         case iro_##a:                                    \
1602                 ops->equivalent_node  = equivalent_node_##a; \
1603                 break
1604
1605         switch (code) {
1606         CASE(Block);
1607         CASE(Jmp);
1608         CASE(Raise);
1609         CASE(Or);
1610         CASE(Add);
1611         CASE(Eor);
1612         CASE(Sub);
1613         CASE(Shl);
1614         CASE(Shr);
1615         CASE(Shrs);
1616         CASE(Rot);
1617         CASE(Not);
1618         CASE(Minus);
1619         CASE(Mul);
1620         CASE(Div);
1621         CASE(Quot);
1622         CASE(DivMod);
1623         CASE(And);
1624         CASE(Conv);
1625         CASE(Cast);
1626         CASE(Phi);
1627         CASE(Sync);
1628         CASE(Proj);
1629         CASE(Id);
1630         CASE(Mux);
1631         CASE(Psi);
1632         CASE(Cmp);
1633         CASE(Confirm);
1634         CASE(CopyB);
1635         CASE(Bound);
1636         default:
1637                 /* leave NULL */;
1638         }
1639
1640         return ops;
1641 #undef CASE
1642 }  /* firm_set_default_equivalent_node */
1643
1644 /**
1645  * Do node specific optimizations of nodes predecessors.
1646  */
1647 static void optimize_preds(ir_node *n) {
1648         switch (get_irn_opcode(n)) {
1649
1650         case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1651                 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1652
1653                 if (get_irn_op(a) == op_Cast) {
1654                         a = get_Cast_op(a);
1655                         set_Cmp_left(n, a);
1656                 }
1657                 if (get_irn_op(b) == op_Cast) {
1658                         b = get_Cast_op(b);
1659                         set_Cmp_right(n, b);
1660                 }
1661                 break;
1662         }
1663
1664         default: break;
1665         } /* end switch */
1666 }  /* optimize_preds */
1667
1668 /**
1669  * Returns non-zero if a node is a Phi node
1670  * with all predecessors constant.
1671  */
1672 static int is_const_Phi(ir_node *n) {
1673         int i;
1674
1675         if (! is_Phi(n))
1676                 return 0;
1677         for (i = get_irn_arity(n) - 1; i >= 0; --i)
1678                 if (! is_Const(get_irn_n(n, i)))
1679                         return 0;
1680                 return 1;
1681 }  /* is_const_Phi */
1682
1683 /**
1684  * Apply an evaluator on a binop with a constant operators (and one Phi).
1685  *
1686  * @param phi    the Phi node
1687  * @param other  the other operand
1688  * @param eval   an evaluator function
1689  * @param left   if non-zero, other is the left operand, else the right
1690  *
1691  * @return a new Phi node if the conversion was successful, NULL else
1692  */
1693 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1694         tarval   *tv;
1695         void     **res;
1696         ir_node  *pred;
1697         ir_mode  *mode;
1698         ir_graph *irg;
1699         int      i, n = get_irn_arity(phi);
1700
1701         NEW_ARR_A(void *, res, n);
1702         if (left) {
1703                 for (i = 0; i < n; ++i) {
1704                         pred = get_irn_n(phi, i);
1705                         tv   = get_Const_tarval(pred);
1706                         tv   = eval(other, tv);
1707
1708                         if (tv == tarval_bad) {
1709                                 /* folding failed, bad */
1710                                 return NULL;
1711                         }
1712                         res[i] = tv;
1713                 }
1714         } else {
1715                 for (i = 0; i < n; ++i) {
1716                         pred = get_irn_n(phi, i);
1717                         tv   = get_Const_tarval(pred);
1718                         tv   = eval(tv, other);
1719
1720                         if (tv == tarval_bad) {
1721                                 /* folding failed, bad */
1722                                 return 0;
1723                         }
1724                         res[i] = tv;
1725                 }
1726         }
1727         mode = get_irn_mode(phi);
1728         irg  = current_ir_graph;
1729         for (i = 0; i < n; ++i) {
1730                 pred = get_irn_n(phi, i);
1731                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1732                         mode, res[i], get_Const_type(pred));
1733         }
1734         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1735 }  /* apply_binop_on_phi */
1736
1737 /**
1738  * Apply an evaluator on a unop with a constant operator (a Phi).
1739  *
1740  * @param phi    the Phi node
1741  * @param eval   an evaluator function
1742  *
1743  * @return a new Phi node if the conversion was successful, NULL else
1744  */
1745 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1746         tarval   *tv;
1747         void     **res;
1748         ir_node  *pred;
1749         ir_mode  *mode;
1750         ir_graph *irg;
1751         int      i, n = get_irn_arity(phi);
1752
1753         NEW_ARR_A(void *, res, n);
1754         for (i = 0; i < n; ++i) {
1755                 pred = get_irn_n(phi, i);
1756                 tv   = get_Const_tarval(pred);
1757                 tv   = eval(tv);
1758
1759                 if (tv == tarval_bad) {
1760                         /* folding failed, bad */
1761                         return 0;
1762                 }
1763                 res[i] = tv;
1764         }
1765         mode = get_irn_mode(phi);
1766         irg  = current_ir_graph;
1767         for (i = 0; i < n; ++i) {
1768                 pred = get_irn_n(phi, i);
1769                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1770                         mode, res[i], get_Const_type(pred));
1771         }
1772         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1773 }  /* apply_unop_on_phi */
1774
1775 /**
1776  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1777  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1778  * If possible, remove the Conv's.
1779  */
1780 static ir_node *transform_node_AddSub(ir_node *n) {
1781         ir_mode *mode = get_irn_mode(n);
1782
1783         if (mode_is_reference(mode)) {
1784                 ir_node *left  = get_binop_left(n);
1785                 ir_node *right = get_binop_right(n);
1786                 int ref_bits   = get_mode_size_bits(mode);
1787
1788                 if (get_irn_op(left) == op_Conv) {
1789                         ir_mode *mode = get_irn_mode(left);
1790                         int bits      = get_mode_size_bits(mode);
1791
1792                         if (ref_bits == bits &&
1793                             mode_is_int(mode) &&
1794                             get_mode_arithmetic(mode) == irma_twos_complement) {
1795                                 ir_node *pre      = get_Conv_op(left);
1796                                 ir_mode *pre_mode = get_irn_mode(pre);
1797
1798                                 if (mode_is_int(pre_mode) &&
1799                                     get_mode_size_bits(pre_mode) == bits &&
1800                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1801                                         /* ok, this conv just changes to sign, moreover the calculation
1802                                          * is done with same number of bits as our address mode, so
1803                                          * we can ignore the conv as address calculation can be viewed
1804                                          * as either signed or unsigned
1805                                          */
1806                                         set_binop_left(n, pre);
1807                                 }
1808                         }
1809                 }
1810
1811                 if (get_irn_op(right) == op_Conv) {
1812                         ir_mode *mode = get_irn_mode(right);
1813                         int bits      = get_mode_size_bits(mode);
1814
1815                         if (ref_bits == bits &&
1816                                 mode_is_int(mode) &&
1817                                 get_mode_arithmetic(mode) == irma_twos_complement) {
1818                                 ir_node *pre      = get_Conv_op(right);
1819                                 ir_mode *pre_mode = get_irn_mode(pre);
1820
1821                                 if (mode_is_int(pre_mode) &&
1822                                     get_mode_size_bits(pre_mode) == bits &&
1823                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1824                                         /* ok, this conv just changes to sign, moreover the calculation
1825                                          * is done with same number of bits as our address mode, so
1826                                          * we can ignore the conv as address calculation can be viewed
1827                                          * as either signed or unsigned
1828                                          */
1829                                         set_binop_right(n, pre);
1830                                 }
1831                         }
1832                 }
1833         }
1834         return n;
1835 }  /* transform_node_AddSub */
1836
1837 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1838   c = NULL;                                                 \
1839   if (is_Const(b) && is_const_Phi(a)) {                     \
1840     /* check for Op(Phi, Const) */                          \
1841     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1842   }                                                         \
1843   else if (is_Const(a) && is_const_Phi(b)) {                \
1844     /* check for Op(Const, Phi) */                          \
1845     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1846   }                                                         \
1847   if (c) {                                                  \
1848     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1849     return c;                                               \
1850   }
1851
1852 #define HANDLE_UNOP_PHI(op,a,c)                 \
1853   c = NULL;                                     \
1854   if (is_const_Phi(a)) {                        \
1855     /* check for Op(Phi) */                     \
1856     c = apply_unop_on_phi(a, op);               \
1857   }                                             \
1858   if (c) {                                      \
1859     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1860     return c;                                   \
1861   }
1862
1863
1864 /**
1865  * Do the AddSub optimization, then Transform
1866  *   Constant folding on Phi
1867  *   Add(a,a)          -> Mul(a, 2)
1868  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1869  * if the mode is integer or float.
1870  * Transform Add(a,-b) into Sub(a,b).
1871  * Reassociation might fold this further.
1872  */
1873 static ir_node *transform_node_Add(ir_node *n) {
1874         ir_mode *mode;
1875         ir_node *a, *b, *c, *oldn = n;
1876
1877         n = transform_node_AddSub(n);
1878
1879         a = get_Add_left(n);
1880         b = get_Add_right(n);
1881
1882         HANDLE_BINOP_PHI(tarval_add, a,b,c);
1883
1884         mode = get_irn_mode(n);
1885
1886         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1887         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1888                 return n;
1889
1890         if (mode_is_num(mode)) {
1891                 if (a == b) {
1892                         ir_node *block = get_irn_n(n, -1);
1893
1894                         n = new_rd_Mul(
1895                                 get_irn_dbg_info(n),
1896                                 current_ir_graph,
1897                                 block,
1898                                 a,
1899                                 new_r_Const_long(current_ir_graph, block, mode, 2),
1900                                 mode);
1901                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1902                 } else if (get_irn_op(a) == op_Minus) {
1903                         n = new_rd_Sub(
1904                                         get_irn_dbg_info(n),
1905                                         current_ir_graph,
1906                                         get_irn_n(n, -1),
1907                                         b,
1908                                         get_Minus_op(a),
1909                                         mode);
1910                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1911                 } else if (get_irn_op(b) == op_Minus) {
1912                         n = new_rd_Sub(
1913                                         get_irn_dbg_info(n),
1914                                         current_ir_graph,
1915                                         get_irn_n(n, -1),
1916                                         a,
1917                                         get_Minus_op(b),
1918                                         mode);
1919                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1920                 }
1921                 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1922                 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1923                         ir_node *ma = get_Mul_left(a);
1924                         ir_node *mb = get_Mul_right(a);
1925
1926                         if (b == ma) {
1927                                 ir_node *blk = get_irn_n(n, -1);
1928                                 n = new_rd_Mul(
1929                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1930                                                 ma,
1931                                                 new_rd_Add(
1932                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1933                                                         mb,
1934                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1935                                                         mode),
1936                                                 mode);
1937                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1938                         } else if (b == mb) {
1939                                 ir_node *blk = get_irn_n(n, -1);
1940                                 n = new_rd_Mul(
1941                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1942                                                 mb,
1943                                                 new_rd_Add(
1944                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1945                                                         ma,
1946                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1947                                                         mode),
1948                                                 mode);
1949                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1950                         }
1951                 }
1952                 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1953                 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1954                         ir_node *ma = get_Mul_left(b);
1955                         ir_node *mb = get_Mul_right(b);
1956
1957                         if (a == ma) {
1958                                 ir_node *blk = get_irn_n(n, -1);
1959                                 n = new_rd_Mul(
1960                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1961                                                 ma,
1962                                                 new_rd_Add(
1963                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1964                                                         mb,
1965                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1966                                                         mode),
1967                                                 mode);
1968                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1969                         } else if (a == mb) {
1970                                 ir_node *blk = get_irn_n(n, -1);
1971                                 n = new_rd_Mul(
1972                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1973                                                 mb,
1974                                                 new_rd_Add(
1975                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1976                                                         ma,
1977                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1978                                                         mode),
1979                                                 mode);
1980                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1981                         }
1982                 }
1983         }
1984         return n;
1985 }  /* transform_node_Add */
1986
1987 /**
1988  * Do the AddSub optimization, then Transform
1989  *   Constant folding on Phi
1990  *   Sub(0,a)          -> Minus(a)
1991  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
1992  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
1993  */
1994 static ir_node *transform_node_Sub(ir_node *n) {
1995         ir_mode *mode;
1996         ir_node *oldn = n;
1997         ir_node *a, *b, *c;
1998
1999         n = transform_node_AddSub(n);
2000
2001         a = get_Sub_left(n);
2002         b = get_Sub_right(n);
2003
2004         HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2005
2006         mode = get_irn_mode(n);
2007
2008         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2009         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2010                 return n;
2011
2012         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2013         if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2014                 n = new_rd_Minus(
2015                                 get_irn_dbg_info(n),
2016                                 current_ir_graph,
2017                                 get_irn_n(n, -1),
2018                                 b,
2019                                 mode);
2020                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2021         }
2022         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2023         else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2024                 ir_node *ma = get_Mul_left(a);
2025                 ir_node *mb = get_Mul_right(a);
2026
2027                 if (ma == b) {
2028                         ir_node *blk = get_irn_n(n, -1);
2029                         n = new_rd_Mul(
2030                                         get_irn_dbg_info(n),
2031                                         current_ir_graph, blk,
2032                                         ma,
2033                                         new_rd_Sub(
2034                                                 get_irn_dbg_info(n),
2035                                                 current_ir_graph, blk,
2036                                                 mb,
2037                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2038                                                 mode),
2039                                         mode);
2040                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2041                 } else if (mb == b) {
2042                         ir_node *blk = get_irn_n(n, -1);
2043                         n = new_rd_Mul(
2044                                         get_irn_dbg_info(n),
2045                                         current_ir_graph, blk,
2046                                         mb,
2047                                         new_rd_Sub(
2048                                                 get_irn_dbg_info(n),
2049                                                 current_ir_graph, blk,
2050                                                 ma,
2051                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2052                                                 mode),
2053                                         mode);
2054                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2055                 }
2056         } else if (get_irn_op(a) == op_Sub) {
2057                 ir_node *x   = get_Sub_left(a);
2058                 ir_node *y   = get_Sub_right(a);
2059                 ir_node *blk = get_irn_n(n, -1);
2060                 ir_mode *m_b = get_irn_mode(b);
2061                 ir_mode *m_y = get_irn_mode(y);
2062                 ir_node *add;
2063
2064                 /* Determine the right mode for the Add. */
2065                 if (m_b == m_y)
2066                         mode = m_b;
2067                 else if (mode_is_reference(m_b))
2068                         mode = m_b;
2069                 else if (mode_is_reference(m_y))
2070                         mode = m_y;
2071                 else {
2072                         /*
2073                          * Both modes are different but none is reference,
2074                          * happens for instance in SubP(SubP(P, Iu), Is).
2075                          * We have two possibilities here: Cast or ignore.
2076                          * Currently we ignore this case.
2077                          */
2078                         return n;
2079                 }
2080
2081                 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2082
2083                 set_Sub_left(n, x);
2084                 set_Sub_right(n, add);
2085                 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2086         }
2087
2088         return n;
2089 }  /* transform_node_Sub */
2090
2091 /**
2092  * Transform Mul(a,-1) into -a.
2093  * Do constant evaluation of Phi nodes.
2094  * Do architecture dependent optimizations on Mul nodes
2095  */
2096 static ir_node *transform_node_Mul(ir_node *n) {
2097         ir_node *c, *oldn = n;
2098         ir_node *a = get_Mul_left(n);
2099         ir_node *b = get_Mul_right(n);
2100         ir_mode *mode;
2101
2102         HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2103
2104         mode = get_irn_mode(n);
2105         if (mode_is_signed(mode)) {
2106                 ir_node *r = NULL;
2107
2108                 if (value_of(a) == get_mode_minus_one(mode))
2109                         r = b;
2110                 else if (value_of(b) == get_mode_minus_one(mode))
2111                         r = a;
2112                 if (r) {
2113                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2114                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2115                         return n;
2116                 }
2117         }
2118         return arch_dep_replace_mul_with_shifts(n);
2119 }  /* transform_node_Mul */
2120
2121 /**
2122  * Transform a Div Node.
2123  */
2124 static ir_node *transform_node_Div(ir_node *n) {
2125         tarval *tv = value_of(n);
2126         ir_node *value = n;
2127
2128         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2129
2130         if (tv != tarval_bad) {
2131                 value = new_Const(get_tarval_mode(tv), tv);
2132
2133                 DBG_OPT_CSTEVAL(n, value);
2134         } else /* Try architecture dependent optimization */
2135                 value = arch_dep_replace_div_by_const(n);
2136
2137         if (value != n) {
2138                 /* Turn Div into a tuple (mem, bad, value) */
2139                 ir_node *mem = get_Div_mem(n);
2140
2141                 turn_into_tuple(n, pn_Div_max);
2142                 set_Tuple_pred(n, pn_Div_M, mem);
2143                 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2144                 set_Tuple_pred(n, pn_Div_res, value);
2145         }
2146         return n;
2147 }  /* transform_node_Div */
2148
2149 /**
2150  * Transform a Mod node.
2151  */
2152 static ir_node *transform_node_Mod(ir_node *n) {
2153         tarval *tv = value_of(n);
2154         ir_node *value = n;
2155
2156         /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2157
2158         if (tv != tarval_bad) {
2159                 value = new_Const(get_tarval_mode(tv), tv);
2160
2161                 DBG_OPT_CSTEVAL(n, value);
2162         } else /* Try architecture dependent optimization */
2163                 value = arch_dep_replace_mod_by_const(n);
2164
2165         if (value != n) {
2166                 /* Turn Mod into a tuple (mem, bad, value) */
2167                 ir_node *mem = get_Mod_mem(n);
2168
2169                 turn_into_tuple(n, pn_Mod_max);
2170                 set_Tuple_pred(n, pn_Mod_M, mem);
2171                 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2172                 set_Tuple_pred(n, pn_Mod_res, value);
2173         }
2174         return n;
2175 }  /* transform_node_Mod */
2176
2177 /**
2178  * Transform a DivMod node.
2179  */
2180 static ir_node *transform_node_DivMod(ir_node *n) {
2181         int evaluated = 0;
2182
2183         ir_node *a = get_DivMod_left(n);
2184         ir_node *b = get_DivMod_right(n);
2185         ir_mode *mode = get_irn_mode(a);
2186         tarval *ta = value_of(a);
2187         tarval *tb = value_of(b);
2188
2189         if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2190                 return n;
2191
2192         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2193
2194         if (tb != tarval_bad) {
2195                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2196                         b = new_Const (mode, get_mode_null(mode));
2197                         evaluated = 1;
2198
2199                         DBG_OPT_CSTEVAL(n, b);
2200                 } else if (ta != tarval_bad) {
2201                         tarval *resa, *resb;
2202                         resa = tarval_div (ta, tb);
2203                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2204                                                              Jmp for X result!? */
2205                         resb = tarval_mod (ta, tb);
2206                         if (resb == tarval_bad) return n; /* Causes exception! */
2207                         a = new_Const (mode, resa);
2208                         b = new_Const (mode, resb);
2209                         evaluated = 1;
2210
2211                         DBG_OPT_CSTEVAL(n, a);
2212                         DBG_OPT_CSTEVAL(n, b);
2213                 } else { /* Try architecture dependent optimization */
2214                         arch_dep_replace_divmod_by_const(&a, &b, n);
2215                         evaluated = a != NULL;
2216                 }
2217         } else if (ta == get_mode_null(mode)) {
2218                 /* 0 / non-Const = 0 */
2219                 b = a;
2220                 evaluated = 1;
2221         }
2222
2223         if (evaluated) { /* replace by tuple */
2224                 ir_node *mem = get_DivMod_mem(n);
2225                 turn_into_tuple(n, pn_DivMod_max);
2226                 set_Tuple_pred(n, pn_DivMod_M,        mem);
2227                 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
2228                 set_Tuple_pred(n, pn_DivMod_res_div,  a);
2229                 set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2230         }
2231
2232         return n;
2233 }  /* transform_node_DivMod */
2234
2235 /**
2236  * Optimize Abs(x) into  x if x is Confirmed >= 0
2237  * Optimize Abs(x) into -x if x is Confirmed <= 0
2238  */
2239 static ir_node *transform_node_Abs(ir_node *n) {
2240         ir_node        *oldn = n;
2241         ir_node        *a = get_Abs_op(n);
2242         value_classify_sign sign = classify_value_sign(a);
2243
2244         if (sign == value_classified_negative) {
2245                 ir_mode *mode = get_irn_mode(n);
2246
2247                 /*
2248                  * We can replace the Abs by -x here.
2249                  * We even could add a new Confirm here.
2250                  *
2251                  * Note that -x would create a new node, so we could
2252                  * not run it in the equivalent_node() context.
2253                  */
2254                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2255                                 get_irn_n(n, -1), a, mode);
2256
2257                 DBG_OPT_CONFIRM(oldn, n);
2258         } else if (sign == value_classified_positive) {
2259                 /* n is positive, Abs is not needed */
2260                 n = a;
2261
2262                 DBG_OPT_CONFIRM(oldn, n);
2263         }
2264
2265         return n;
2266 }  /* transform_node_Abs */
2267
2268 /**
2269  * Transform a Cond node.
2270  *
2271  * Replace the Cond by a Jmp if it branches on a constant
2272  * condition.
2273  */
2274 static ir_node *transform_node_Cond(ir_node *n) {
2275
2276         ir_node *jmp;
2277         ir_node *a = get_Cond_selector(n);
2278         tarval *ta = value_of(a);
2279
2280         /* we need block info which is not available in floating irgs */
2281         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2282                 return n;
2283
2284         if ((ta != tarval_bad) &&
2285             (get_irn_mode(a) == mode_b) &&
2286             (get_opt_unreachable_code())) {
2287                 /* It's a boolean Cond, branching on a boolean constant.
2288                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2289                 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2290                 turn_into_tuple(n, pn_Cond_max);
2291                 if (ta == tarval_b_true) {
2292                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2293                         set_Tuple_pred(n, pn_Cond_true, jmp);
2294                 } else {
2295                         set_Tuple_pred(n, pn_Cond_false, jmp);
2296                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2297                 }
2298                 /* We might generate an endless loop, so keep it alive. */
2299                 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2300         }
2301         return n;
2302 }  /* transform_node_Cond */
2303
2304 /**
2305  * Transform an And.
2306  */
2307 static ir_node *transform_node_And(ir_node *n) {
2308         ir_node *c, *oldn = n;
2309         ir_node *a = get_And_left(n);
2310         ir_node *b = get_And_right(n);
2311
2312         HANDLE_BINOP_PHI(tarval_and, a,b,c);
2313         return n;
2314 }  /* transform_node_And */
2315
2316 /**
2317  * Transform an Eor.
2318  */
2319 static ir_node *transform_node_Eor(ir_node *n) {
2320         ir_node *c, *oldn = n;
2321         ir_node *a = get_Eor_left(n);
2322         ir_node *b = get_Eor_right(n);
2323         ir_mode *mode = get_irn_mode(n);
2324
2325         HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2326
2327         if (a == b) {
2328                 /* a ^ a = 0 */
2329                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2330                                  mode, get_mode_null(mode));
2331                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2332         } else if ((mode == mode_b)
2333                    && (get_irn_op(a) == op_Proj)
2334                    && (get_irn_mode(a) == mode_b)
2335                    && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2336                    && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2337                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2338                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2339                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2340
2341                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2342         } else if ((mode == mode_b)
2343                && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2344                 /* The Eor is a Not. Replace it by a Not. */
2345                 /*   ????!!!Extend to bitfield 1111111. */
2346                 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2347
2348                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2349         }
2350
2351         return n;
2352 }  /* transform_node_Eor */
2353
2354 /**
2355  * Transform a Not.
2356  */
2357 static ir_node *transform_node_Not(ir_node *n) {
2358         ir_node *c, *oldn = n;
2359         ir_node *a = get_Not_op(n);
2360
2361         HANDLE_UNOP_PHI(tarval_not,a,c);
2362
2363         /* check for a boolean Not */
2364         if (   (get_irn_mode(n) == mode_b)
2365             && (get_irn_op(a) == op_Proj)
2366             && (get_irn_mode(a) == mode_b)
2367             && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2368                 /* We negate a Cmp. The Cmp has the negated result anyways! */
2369                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2370                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2371                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2372         }
2373         return n;
2374 }  /* transform_node_Not */
2375
2376 /**
2377  * Transform a Minus.
2378  */
2379 static ir_node *transform_node_Minus(ir_node *n) {
2380         ir_node *c, *oldn = n;
2381         ir_node *a = get_Minus_op(n);
2382
2383         HANDLE_UNOP_PHI(tarval_neg,a,c);
2384         return n;
2385 }  /* transform_node_Minus */
2386
2387 /**
2388  * Transform a Cast_type(Const) into a new Const_type
2389  */
2390 static ir_node *transform_node_Cast(ir_node *n) {
2391         ir_node *oldn = n;
2392         ir_node *pred = get_Cast_op(n);
2393         ir_type *tp = get_irn_type(n);
2394
2395         if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2396                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2397                         get_Const_tarval(pred), tp);
2398                 DBG_OPT_CSTEVAL(oldn, n);
2399         } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2400                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2401                         get_SymConst_kind(pred), tp);
2402                 DBG_OPT_CSTEVAL(oldn, n);
2403         }
2404
2405         return n;
2406 }  /* transform_node_Cast */
2407
2408 /**
2409  * Transform a Proj(Div) with a non-zero value.
2410  * Removes the exceptions and routes the memory to the NoMem node.
2411  */
2412 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2413         ir_node *n = get_Proj_pred(proj);
2414         ir_node *b = get_Div_right(n);
2415         ir_node *confirm;
2416         long proj_nr;
2417
2418         if (value_not_zero(b, &confirm)) {
2419                 /* div(x, y) && y != 0 */
2420                 proj_nr = get_Proj_proj(proj);
2421                 if (proj_nr == pn_Div_X_except) {
2422                         /* we found an exception handler, remove it */
2423                         DBG_OPT_EXC_REM(proj);
2424                         return new_Bad();
2425                 } else if (proj_nr == pn_Div_M) {
2426                         ir_node *res = get_Div_mem(n);
2427                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2428
2429                         if (confirm) {
2430                                 /* This node can only float up to the Confirm block */
2431                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2432                         }
2433                         set_irn_pinned(n, op_pin_state_floats);
2434                         /* this is a Div without exception, we can remove the memory edge */
2435                         set_Div_mem(n, new_mem);
2436                         return res;
2437                 }
2438         }
2439         return proj;
2440 }  /* transform_node_Proj_Div */
2441
2442 /**
2443  * Transform a Proj(Mod) with a non-zero value.
2444  * Removes the exceptions and routes the memory to the NoMem node.
2445  */
2446 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2447         ir_node *n = get_Proj_pred(proj);
2448         ir_node *b = get_Mod_right(n);
2449         ir_node *confirm;
2450         long proj_nr;
2451
2452         if (value_not_zero(b, &confirm)) {
2453                 /* mod(x, y) && y != 0 */
2454                 proj_nr = get_Proj_proj(proj);
2455
2456                 if (proj_nr == pn_Mod_X_except) {
2457                         /* we found an exception handler, remove it */
2458                         DBG_OPT_EXC_REM(proj);
2459                         return new_Bad();
2460                 } else if (proj_nr == pn_Mod_M) {
2461                         ir_node *res = get_Mod_mem(n);
2462                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2463
2464                         if (confirm) {
2465                                 /* This node can only float up to the Confirm block */
2466                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2467                         }
2468                         set_irn_pinned(n, op_pin_state_floats);
2469                         /* this is a Mod without exception, we can remove the memory edge */
2470                         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2471                         return res;
2472                 } else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2473                         /* a % a = 0 if a != 0 */
2474                         ir_mode *mode = get_irn_mode(proj);
2475                         ir_node *res  = new_Const(mode, get_mode_null(mode));
2476
2477                         DBG_OPT_CSTEVAL(n, res);
2478                         return res;
2479                 }
2480         }
2481         return proj;
2482 }  /* transform_node_Proj_Mod */
2483
2484 /**
2485  * Transform a Proj(DivMod) with a non-zero value.
2486  * Removes the exceptions and routes the memory to the NoMem node.
2487  */
2488 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2489         ir_node *n = get_Proj_pred(proj);
2490         ir_node *b = get_DivMod_right(n);
2491         ir_node *confirm;
2492         long proj_nr;
2493
2494         if (value_not_zero(b, &confirm)) {
2495                 /* DivMod(x, y) && y != 0 */
2496                 proj_nr = get_Proj_proj(proj);
2497
2498                 if (proj_nr == pn_DivMod_X_except) {
2499                         /* we found an exception handler, remove it */
2500                         DBG_OPT_EXC_REM(proj);
2501                         return new_Bad();
2502                 } else if (proj_nr == pn_DivMod_M) {
2503                         ir_node *res = get_DivMod_mem(n);
2504                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2505
2506                         if (confirm) {
2507                                 /* This node can only float up to the Confirm block */
2508                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2509                         }
2510                         set_irn_pinned(n, op_pin_state_floats);
2511                         /* this is a DivMod without exception, we can remove the memory edge */
2512                         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2513                         return res;
2514                 } else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2515                         /* a % a = 0 if a != 0 */
2516                         ir_mode *mode = get_irn_mode(proj);
2517                         ir_node *res  = new_Const(mode, get_mode_null(mode));
2518
2519                         DBG_OPT_CSTEVAL(n, res);
2520                         return res;
2521                 }
2522         }
2523         return proj;
2524 }  /* transform_node_Proj_DivMod */
2525
2526 /**
2527  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2528  */
2529 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2530         if (get_opt_unreachable_code()) {
2531                 ir_node *n = get_Proj_pred(proj);
2532                 ir_node *b = get_Cond_selector(n);
2533
2534                 if (mode_is_int(get_irn_mode(b))) {
2535                         tarval *tb = value_of(b);
2536
2537                         if (tb != tarval_bad) {
2538                                 /* we have a constant switch */
2539                                 long num = get_Proj_proj(proj);
2540
2541                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2542                                         if (get_tarval_long(tb) == num) {
2543                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
2544                                                  * in a block. This case is optimized away in optimize_cf(). */
2545                                                 return proj;
2546                                         } else {
2547                                                 /* this case will NEVER be taken, kill it */
2548                                                 return new_Bad();
2549                                         }
2550                                 }
2551                         }
2552                 }
2553         }
2554         return proj;
2555 }  /* transform_node_Proj_Cond */
2556
2557 /**
2558  * Normalizes and optimizes Cmp nodes.
2559  */
2560 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2561         if (get_opt_reassociation()) {
2562                 ir_node *n     = get_Proj_pred(proj);
2563                 ir_node *left  = get_Cmp_left(n);
2564                 ir_node *right = get_Cmp_right(n);
2565                 ir_node *c     = NULL;
2566                 tarval *tv     = NULL;
2567                 int changed    = 0;
2568                 ir_mode *mode  = NULL;
2569                 long proj_nr   = get_Proj_proj(proj);
2570
2571                 /*
2572                  * First step: normalize the compare op
2573                  * by placing the constant on the right site
2574                  * or moving the lower address node to the left.
2575                  * We ignore the case that both are constants
2576                  * this case should be optimized away.
2577                  */
2578                 if (get_irn_op(right) == op_Const) {
2579                         c = right;
2580                 } else if (get_irn_op(left) == op_Const) {
2581                         c     = left;
2582                         left  = right;
2583                         right = c;
2584
2585                         proj_nr = get_inversed_pnc(proj_nr);
2586                         changed |= 1;
2587                 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2588                         ir_node *t = left;
2589
2590                         left  = right;
2591                         right = t;
2592
2593                         proj_nr = get_inversed_pnc(proj_nr);
2594                         changed |= 1;
2595                 }
2596
2597                 /*
2598                  * Second step: Try to reduce the magnitude
2599                  * of a constant. This may help to generate better code
2600                  * later and may help to normalize more compares.
2601                  * Of course this is only possible for integer values.
2602                  */
2603                 if (c) {
2604                         mode = get_irn_mode(c);
2605                         tv = get_Const_tarval(c);
2606
2607                         if (tv != tarval_bad) {
2608                                 /* the following optimization is possible on modes without Overflow
2609                                  * on Unary Minus or on == and !=:
2610                                  * -a CMP c  ==>  a swap(CMP) -c
2611                                  *
2612                                  * Beware: for two-complement Overflow may occur, so only == and != can
2613                                  * be optimized, see this:
2614                                  * -MININT < 0 =/=> MININT > 0 !!!
2615                                  */
2616                                 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2617                                     (!mode_overflow_on_unary_Minus(mode) ||
2618                                     (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2619                                         left = get_Minus_op(left);
2620                                         tv = tarval_sub(get_mode_null(mode), tv);
2621
2622                                         proj_nr = get_inversed_pnc(proj_nr);
2623                                         changed |= 2;
2624                                 }
2625
2626                                 /* for integer modes, we have more */
2627                                 if (mode_is_int(mode)) {
2628                                         /* Ne includes Unordered which is not possible on integers.
2629                                          * However, frontends often use this wrong, so fix it here */
2630                                         if (proj_nr & pn_Cmp_Uo) {
2631                                                 proj_nr &= ~pn_Cmp_Uo;
2632                                                 set_Proj_proj(proj, proj_nr);
2633                                         }
2634
2635                                         /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2636                                         if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2637                                                 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2638                                                 tv = tarval_sub(tv, get_mode_one(mode));
2639
2640                                                 proj_nr ^= pn_Cmp_Eq;
2641                                                 changed |= 2;
2642                                         }
2643                                         /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2644                                         else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2645                                                 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2646                                                 tv = tarval_add(tv, get_mode_one(mode));
2647
2648                                                 proj_nr ^= pn_Cmp_Eq;
2649                                                 changed |= 2;
2650                                         }
2651
2652                                         /* the following reassociations work only for == and != */
2653                                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2654
2655                                                 /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2656                                                 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2657                                                         right = get_Sub_right(left);
2658                                                         left  = get_Sub_left(left);
2659
2660                                                         tv = value_of(right);
2661                                                         changed = 1;
2662                                                 }
2663
2664                                                 if (tv != tarval_bad) {
2665                                                         ir_op *op = get_irn_op(left);
2666
2667                                                         /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2668                                                         if (op == op_Sub) {
2669                                                                 ir_node *c1 = get_Sub_right(left);
2670                                                                 tarval *tv2 = value_of(c1);
2671
2672                                                                 if (tv2 != tarval_bad) {
2673                                                                         tv2 = tarval_add(tv, value_of(c1));
2674
2675                                                                         if (tv2 != tarval_bad) {
2676                                                                                 left    = get_Sub_left(left);
2677                                                                                 tv      = tv2;
2678                                                                                 changed |= 2;
2679                                                                         }
2680                                                                 }
2681                                                         }
2682                                                         /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2683                                                         else if (op == op_Add) {
2684                                                                 ir_node *a_l = get_Add_left(left);
2685                                                                 ir_node *a_r = get_Add_right(left);
2686                                                                 ir_node *a;
2687                                                                 tarval *tv2;
2688
2689                                                                 if (get_irn_op(a_l) == op_Const) {
2690                                                                         a = a_r;
2691                                                                         tv2 = value_of(a_l);
2692                                                                 } else {
2693                                                                         a = a_l;
2694                                                                         tv2 = value_of(a_r);
2695                                                                 }
2696
2697                                                                 if (tv2 != tarval_bad) {
2698                                                                         tv2 = tarval_sub(tv, tv2);
2699
2700                                                                         if (tv2 != tarval_bad) {
2701                                                                                 left    = a;
2702                                                                                 tv      = tv2;
2703                                                                                 changed |= 2;
2704                                                                         }
2705                                                                 }
2706                                                         }
2707                                                         /* -a == c ==> a == -c, -a != c ==> a != -c */
2708                                                         else if (op == op_Minus) {
2709                                                                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2710
2711                                                                 if (tv2 != tarval_bad) {
2712                                                                         left    = get_Minus_op(left);
2713                                                                         tv      = tv2;
2714                                                                         changed |= 2;
2715                                                                 }
2716                                                         }
2717                                                 }
2718                                         } /* == or != */
2719                                         /* the following reassociations work only for <= */
2720                                         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2721                                                 if (tv != tarval_bad) {
2722                                                         ir_op *op = get_irn_op(left);
2723
2724                                                         /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2725                                                         if (op == op_Abs) {
2726                                                         }
2727                                                 }
2728                                         }
2729                                 } /* mode_is_int */
2730
2731                                 /*
2732                                  * optimization for AND:
2733                                  * Optimize:
2734                                  *   And(x, C) == C  ==>  And(x, C) != 0
2735                                  *   And(x, C) != C  ==>  And(X, C) == 0
2736                                  *
2737                                  * if C is a single Bit constant.
2738                                  */
2739                                 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2740                                     (get_irn_op(left) == op_And)) {
2741                                         if (is_single_bit_tarval(tv)) {
2742                                                 /* check for Constant's match. We have check hare the tarvals,
2743                                                    because our const might be changed */
2744                                                 ir_node *la = get_And_left(left);
2745                                                 ir_node *ra = get_And_right(left);
2746                                                 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2747                                                     (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2748                                                                 /* fine: do the transformation */
2749                                                                 tv = get_mode_null(get_tarval_mode(tv));
2750                                                                 proj_nr ^= pn_Cmp_Leg;
2751                                                                 changed |= 2;
2752                                                 }
2753                                         }
2754                                 }
2755                         } /* tarval != bad */
2756                 }
2757
2758                 if (changed) {
2759                         ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2760
2761                         if (changed & 2)      /* need a new Const */
2762                                 right = new_Const(mode, tv);
2763
2764                         /* create a new compare */
2765                         n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2766                                 left, right);
2767
2768                         set_Proj_pred(proj, n);
2769                         set_Proj_proj(proj, proj_nr);
2770                 }
2771         }
2772         return proj;
2773 }  /* transform_node_Proj_Cmp */
2774
2775 /**
2776  * Does all optimizations on nodes that must be done on it's Proj's
2777  * because of creating new nodes.
2778  */
2779 static ir_node *transform_node_Proj(ir_node *proj) {
2780         ir_node *n = get_Proj_pred(proj);
2781
2782         switch (get_irn_opcode(n)) {
2783         case iro_Div:
2784                 return transform_node_Proj_Div(proj);
2785
2786         case iro_Mod:
2787                 return transform_node_Proj_Mod(proj);
2788
2789         case iro_DivMod:
2790                 return transform_node_Proj_DivMod(proj);
2791
2792         case iro_Cond:
2793                 return transform_node_Proj_Cond(proj);
2794
2795         case iro_Cmp:
2796                 return transform_node_Proj_Cmp(proj);
2797
2798         case iro_Tuple:
2799                 /* should not happen, but if it does will be optimized away */
2800                 return equivalent_node_Proj(proj);
2801
2802         default:
2803                 /* do nothing */
2804                 return proj;
2805         }
2806 }  /* transform_node_Proj */
2807
2808 /**
2809  * Move Confirms down through Phi nodes.
2810  */
2811 static ir_node *transform_node_Phi(ir_node *phi) {
2812         int i, n;
2813         ir_mode *mode = get_irn_mode(phi);
2814
2815         if (mode_is_reference(mode)) {
2816                 n = get_irn_arity(phi);
2817
2818                 /* Beware of Phi0 */
2819                 if (n > 0) {
2820                         ir_node *pred = get_irn_n(phi, 0);
2821                         ir_node *bound, *new_Phi, *block, **in;
2822                         pn_Cmp  pnc;
2823
2824                         if (! is_Confirm(pred))
2825                                 return phi;
2826
2827                         bound = get_Confirm_bound(pred);
2828                         pnc   = get_Confirm_cmp(pred);
2829
2830                         NEW_ARR_A(ir_node *, in, n);
2831                         in[0] = get_Confirm_value(pred);
2832
2833                         for (i = 1; i < n; ++i) {
2834                                 pred = get_irn_n(phi, i);
2835
2836                                 if (! is_Confirm(pred) ||
2837                                         get_Confirm_bound(pred) != bound ||
2838                                         get_Confirm_cmp(pred) != pnc)
2839                                         return phi;
2840                                 in[i] = get_Confirm_value(pred);
2841                         }
2842                         /* move the Confirm nodes "behind" the Phi */
2843                         block = get_irn_n(phi, -1);
2844                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2845                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2846                 }
2847         }
2848         return phi;
2849 }  /* transform_node_Phi */
2850
2851 /**
2852  * Returns the operands of a commutative bin-op, if one operand is
2853  * a const, it is returned as the second one.
2854  */
2855 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
2856         ir_node *op_a = get_binop_left(binop);
2857         ir_node *op_b = get_binop_right(binop);
2858
2859         assert(is_op_commutative(get_irn_op(binop)));
2860
2861         if (get_irn_op(op_a) == op_Const) {
2862                 *a = op_b;
2863                 *c = op_a;
2864         } else {
2865                 *a = op_a;
2866                 *c = op_b;
2867         }
2868 }  /* get_comm_Binop_Ops */
2869
2870 /**
2871  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2872  * Such pattern may arise in bitfield stores.
2873  *
2874  * value  c4                  value      c4 & c2
2875  *    AND     c3                    AND           c1 | c3
2876  *        OR     c2      ===>               OR
2877  *           AND    c1
2878  *               OR
2879  */
2880 static ir_node *transform_node_Or_bf_store(ir_node *or) {
2881         ir_node *and, *c1;
2882         ir_node *or_l, *c2;
2883         ir_node *and_l, *c3;
2884         ir_node *value, *c4;
2885         ir_node *new_and, *new_const, *block;
2886         ir_mode *mode = get_irn_mode(or);
2887
2888         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2889
2890         get_comm_Binop_Ops(or, &and, &c1);
2891         if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2892                 return or;
2893
2894         get_comm_Binop_Ops(and, &or_l, &c2);
2895         if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2896                 return or;
2897
2898         get_comm_Binop_Ops(or_l, &and_l, &c3);
2899         if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2900                 return or;
2901
2902         get_comm_Binop_Ops(and_l, &value, &c4);
2903         if (get_irn_op(c4) != op_Const)
2904                 return or;
2905
2906         /* ok, found the pattern, check for conditions */
2907         assert(mode == get_irn_mode(and));
2908         assert(mode == get_irn_mode(or_l));
2909         assert(mode == get_irn_mode(and_l));
2910
2911         tv1 = get_Const_tarval(c1);
2912         tv2 = get_Const_tarval(c2);
2913         tv3 = get_Const_tarval(c3);
2914         tv4 = get_Const_tarval(c4);
2915
2916         tv = tarval_or(tv4, tv2);
2917         if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2918                 /* have at least one 0 at the same bit position */
2919                 return or;
2920         }
2921
2922         n_tv4 = tarval_not(tv4);
2923         if (tv3 != tarval_and(tv3, n_tv4)) {
2924                 /* bit in the or_mask is outside the and_mask */
2925                 return or;
2926         }
2927
2928         n_tv2 = tarval_not(tv2);
2929         if (tv1 != tarval_and(tv1, n_tv2)) {
2930                 /* bit in the or_mask is outside the and_mask */
2931                 return or;
2932         }
2933
2934         /* ok, all conditions met */
2935         block = get_irn_n(or, -1);
2936
2937         new_and = new_r_And(current_ir_graph, block,
2938                 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2939
2940         new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2941
2942         set_Or_left(or, new_and);
2943         set_Or_right(or, new_const);
2944
2945         /* check for more */
2946         return transform_node_Or_bf_store(or);
2947 }  /* transform_node_Or_bf_store */
2948
2949 /**
2950  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2951  */
2952 static ir_node *transform_node_Or_Rot(ir_node *or) {
2953         ir_mode *mode = get_irn_mode(or);
2954         ir_node *shl, *shr, *block;
2955         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2956         tarval *tv1, *tv2;
2957
2958         if (! mode_is_int(mode))
2959                 return or;
2960
2961         shl = get_binop_left(or);
2962         shr = get_binop_right(or);
2963
2964         if (get_irn_op(shl) == op_Shr) {
2965                 if (get_irn_op(shr) != op_Shl)
2966                         return or;
2967
2968                 irn = shl;
2969                 shl = shr;
2970                 shr = irn;
2971         } else if (get_irn_op(shl) != op_Shl) {
2972                 return or;
2973         } else if (get_irn_op(shr) != op_Shr) {
2974                 return or;
2975         }
2976         x = get_Shl_left(shl);
2977         if (x != get_Shr_left(shr))
2978                 return or;
2979
2980         c1 = get_Shl_right(shl);
2981         c2 = get_Shr_right(shr);
2982         if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2983                 tv1 = get_Const_tarval(c1);
2984                 if (! tarval_is_long(tv1))
2985                         return or;
2986
2987                 tv2 = get_Const_tarval(c2);
2988                 if (! tarval_is_long(tv2))
2989                         return or;
2990
2991                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2992                         != get_mode_size_bits(mode))
2993                         return or;
2994
2995                 /* yet, condition met */
2996                 block = get_irn_n(or, -1);
2997
2998                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2999
3000                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3001                 return n;
3002         } else if (get_irn_op(c1) == op_Sub) {
3003                 v   = c2;
3004                 sub = c1;
3005
3006                 if (get_Sub_right(sub) != v)
3007                         return or;
3008
3009                 c1 = get_Sub_left(sub);
3010                 if (get_irn_op(c1) != op_Const)
3011                         return or;
3012
3013                 tv1 = get_Const_tarval(c1);
3014                 if (! tarval_is_long(tv1))
3015                         return or;
3016
3017                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3018                         return or;
3019
3020                 /* yet, condition met */
3021                 block = get_nodes_block(or);
3022
3023                 /* a Rot right is not supported, so use a rot left */
3024                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
3025
3026                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3027                 return n;
3028         } else if (get_irn_op(c2) == op_Sub) {
3029                 v   = c1;
3030                 sub = c2;
3031
3032                 c1 = get_Sub_left(sub);
3033                 if (get_irn_op(c1) != op_Const)
3034                         return or;
3035
3036                 tv1 = get_Const_tarval(c1);
3037                 if (! tarval_is_long(tv1))
3038                         return or;
3039
3040                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3041                         return or;
3042
3043                 /* yet, condition met */
3044                 block = get_irn_n(or, -1);
3045
3046                 /* a Rot Left */
3047                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3048
3049                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3050                 return n;
3051         }
3052
3053         return or;
3054 }  /* transform_node_Or_Rot */
3055
3056 /**
3057  * Transform an Or.
3058  */
3059 static ir_node *transform_node_Or(ir_node *n) {
3060         ir_node *c, *oldn = n;
3061         ir_node *a = get_Or_left(n);
3062         ir_node *b = get_Or_right(n);
3063
3064         HANDLE_BINOP_PHI(tarval_or, a,b,c);
3065
3066         n = transform_node_Or_bf_store(n);
3067         n = transform_node_Or_Rot(n);
3068
3069         return n;
3070 }  /* transform_node_Or */
3071
3072
3073 /* forward */
3074 static ir_node *transform_node(ir_node *n);
3075
3076 /**
3077  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3078  *
3079  * Should be moved to reassociation?
3080  */
3081 static ir_node *transform_node_shift(ir_node *n) {
3082         ir_node *left, *right;
3083         tarval *tv1, *tv2, *res;
3084         ir_mode *mode;
3085         int modulo_shf, flag;
3086
3087         left = get_binop_left(n);
3088
3089         /* different operations */
3090         if (get_irn_op(left) != get_irn_op(n))
3091                 return n;
3092
3093         right = get_binop_right(n);
3094         tv1 = value_of(right);
3095         if (tv1 == tarval_bad)
3096                 return n;
3097
3098         tv2 = value_of(get_binop_right(left));
3099         if (tv2 == tarval_bad)
3100                 return n;
3101
3102         res = tarval_add(tv1, tv2);
3103
3104         /* beware: a simple replacement works only, if res < modulo shift */
3105         mode = get_irn_mode(n);
3106
3107         flag = 0;
3108
3109         modulo_shf = get_mode_modulo_shift(mode);
3110         if (modulo_shf > 0) {
3111                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3112
3113                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3114                         flag = 1;
3115         } else
3116                 flag = 1;
3117
3118         if (flag) {
3119                 /* ok, we can replace it */
3120                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3121
3122                 in[0] = get_binop_left(left);
3123                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3124
3125                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3126
3127                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3128
3129                 return transform_node(irn);
3130         }
3131         return n;
3132 }  /* transform_node_shift */
3133
3134 /**
3135  * Transform a Shr.
3136  */
3137 static ir_node *transform_node_Shr(ir_node *n) {
3138         ir_node *c, *oldn = n;
3139         ir_node *a = get_Shr_left(n);
3140         ir_node *b = get_Shr_right(n);
3141
3142         HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3143         return transform_node_shift(n);
3144 }  /* transform_node_Shr */
3145
3146 /**
3147  * Transform a Shrs.
3148  */
3149 static ir_node *transform_node_Shrs(ir_node *n) {
3150         ir_node *c, *oldn = n;
3151         ir_node *a = get_Shrs_left(n);
3152         ir_node *b = get_Shrs_right(n);
3153
3154         HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3155         return transform_node_shift(n);
3156 }  /* transform_node_Shrs */
3157
3158 /**
3159  * Transform a Shl.
3160  */
3161 static ir_node *transform_node_Shl(ir_node *n) {
3162         ir_node *c, *oldn = n;
3163         ir_node *a = get_Shl_left(n);
3164         ir_node *b = get_Shl_right(n);
3165
3166         HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3167         return transform_node_shift(n);
3168 }  /* transform_node_Shl */
3169
3170 /**
3171  * Remove dead blocks and nodes in dead blocks
3172  * in keep alive list.  We do not generate a new End node.
3173  */
3174 static ir_node *transform_node_End(ir_node *n) {
3175         int i, n_keepalives = get_End_n_keepalives(n);
3176
3177         for (i = 0; i < n_keepalives; ++i) {
3178                 ir_node *ka = get_End_keepalive(n, i);
3179                 if (is_Block(ka)) {
3180                         if (is_Block_dead(ka)) {
3181                                 set_End_keepalive(n, i, new_Bad());
3182                         }
3183                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3184                         set_End_keepalive(n, i, new_Bad());
3185         }
3186         return n;
3187 }  /* transform_node_End */
3188
3189 /**
3190  * Optimize a Mux into some simpler cases.
3191  */
3192 static ir_node *transform_node_Mux(ir_node *n) {
3193         ir_node *oldn = n, *sel = get_Mux_sel(n);
3194         ir_mode *mode = get_irn_mode(n);
3195
3196         if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3197                 ir_node *cmp = get_Proj_pred(sel);
3198                 long proj_nr = get_Proj_proj(sel);
3199                 ir_node *f   =  get_Mux_false(n);
3200                 ir_node *t   = get_Mux_true(n);
3201
3202                 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3203                         ir_node *block = get_irn_n(n, -1);
3204
3205                         /*
3206                          * Note: normalization puts the constant on the right site,
3207                          * so we check only one case.
3208                          *
3209                          * Note further that these optimization work even for floating point
3210                          * with NaN's because -NaN == NaN.
3211                          * However, if +0 and -0 is handled differently, we cannot use the first one.
3212                          */
3213                         if (get_irn_op(f) == op_Minus &&
3214                                 get_Minus_op(f)   == t &&
3215                                 get_Cmp_left(cmp) == t) {
3216
3217                                 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3218                                         /* Mux(a >=/> 0, -a, a)  ==>  Abs(a) */
3219                                         n = new_rd_Abs(get_irn_dbg_info(n),
3220                                                 current_ir_graph,
3221                                                 block,
3222                                                 t, mode);
3223                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3224                                         return n;
3225                                 } else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3226                                         /* Mux(a <=/< 0, -a, a)  ==>  Minus(Abs(a)) */
3227                                         n = new_rd_Abs(get_irn_dbg_info(n),
3228                                                         current_ir_graph,
3229                                                         block,
3230                                                         t, mode);
3231                                         n = new_rd_Minus(get_irn_dbg_info(n),
3232                                                         current_ir_graph,
3233                                                         block,
3234                                                         n, mode);
3235
3236                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3237                                         return n;
3238                                 }
3239                         } else if (get_irn_op(t) == op_Minus &&
3240                                    get_Minus_op(t)   == f &&
3241                                    get_Cmp_left(cmp) == f) {
3242
3243                                 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3244                                         /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
3245                                         n = new_rd_Abs(get_irn_dbg_info(n),
3246                                                         current_ir_graph,
3247                                                         block,
3248                                                         f, mode);
3249                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3250                                         return n;
3251                                 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3252                                         /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
3253                                         n = new_rd_Abs(get_irn_dbg_info(n),
3254                                                         current_ir_graph,
3255                                                         block,
3256                                                         f, mode);
3257                                         n = new_rd_Minus(get_irn_dbg_info(n),
3258                                                         current_ir_graph,
3259                                                         block,
3260                                                         n, mode);
3261
3262                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3263                                         return n;
3264                                 }
3265                         }
3266
3267                         if (mode_is_int(mode) && mode_is_signed(mode) &&
3268                                 get_mode_arithmetic(mode) == irma_twos_complement) {
3269                                 ir_node *x = get_Cmp_left(cmp);
3270
3271                                 /* the following optimization works only with signed integer two-complement mode */
3272
3273                                 if (mode == get_irn_mode(x)) {
3274                                         /*
3275                                          * FIXME: this restriction is two rigid, as it would still
3276                                          * work if mode(x) = Hs and mode == Is, but at least it removes
3277                                          * all wrong cases.
3278                                          */
3279                                         if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3280                                             classify_Const(t) == CNST_ALL_ONE &&
3281                                             classify_Const(f) == CNST_NULL) {
3282                                                 /*
3283                                                  * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3284                                                  * Conditions:
3285                                                  * T must be signed.
3286                                                  */
3287                                                 n = new_rd_Shrs(get_irn_dbg_info(n),
3288                                                                 current_ir_graph, block, x,
3289                                                                 new_r_Const_long(current_ir_graph, block, mode_Iu,
3290                                                                 get_mode_size_bits(mode) - 1),
3291                                                                 mode);
3292                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3293                                                 return n;
3294                                         } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3295                                                    classify_Const(t) == CNST_ONE &&
3296                                                    classify_Const(f) == CNST_NULL) {
3297                                                 /*
3298                                                  * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3299                                                  * Conditions:
3300                                                  * T must be signed.
3301                                                  */
3302                                                 n = new_rd_Shr(get_irn_dbg_info(n),
3303                                                         current_ir_graph, block,
3304                                                         new_r_Minus(current_ir_graph, block, x, mode),
3305                                                         new_r_Const_long(current_ir_graph, block, mode_Iu,
3306                                                         get_mode_size_bits(mode) - 1),
3307                                                         mode);
3308                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3309                                                 return n;
3310                                         }
3311                                 }
3312                         }
3313                 }
3314         }
3315         return arch_transform_node_Mux(n);
3316 }  /* transform_node_Mux */
3317
3318 /**
3319  * Optimize a Psi into some simpler cases.
3320  */
3321 static ir_node *transform_node_Psi(ir_node *n) {
3322         if (is_Mux(n))
3323                 return transform_node_Mux(n);
3324
3325         return n;
3326 }  /* transform_node_Psi */
3327
3328 /**
3329  * Tries several [inplace] [optimizing] transformations and returns an
3330  * equivalent node.  The difference to equivalent_node() is that these
3331  * transformations _do_ generate new nodes, and thus the old node must
3332  * not be freed even if the equivalent node isn't the old one.
3333  */
3334 static ir_node *transform_node(ir_node *n) {
3335         if (n->op->ops.transform_node)
3336                 n = n->op->ops.transform_node(n);
3337         return n;
3338 }  /* transform_node */
3339
3340 /**
3341  * Sets the default transform node operation for an ir_op_ops.
3342  *
3343  * @param code   the opcode for the default operation
3344  * @param ops    the operations initialized
3345  *
3346  * @return
3347  *    The operations.
3348  */
3349 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3350 {
3351 #define CASE(a)                                 \
3352         case iro_##a:                                 \
3353                 ops->transform_node  = transform_node_##a;  \
3354                 break
3355
3356         switch (code) {
3357         CASE(Add);
3358         CASE(Sub);
3359         CASE(Mul);
3360         CASE(Div);
3361         CASE(Mod);
3362         CASE(DivMod);
3363         CASE(Abs);
3364         CASE(Cond);
3365         CASE(And);
3366         CASE(Or);
3367         CASE(Eor);
3368         CASE(Minus);
3369         CASE(Not);
3370         CASE(Cast);
3371         CASE(Proj);
3372         CASE(Phi);
3373         CASE(Sel);
3374         CASE(Shr);
3375         CASE(Shrs);
3376         CASE(Shl);
3377         CASE(End);
3378         CASE(Mux);
3379         CASE(Psi);
3380         default:
3381           /* leave NULL */;
3382         }
3383
3384         return ops;
3385 #undef CASE
3386 }  /* firm_set_default_transform_node */
3387
3388
3389 /* **************** Common Subexpression Elimination **************** */
3390
3391 /** The size of the hash table used, should estimate the number of nodes
3392     in a graph. */
3393 #define N_IR_NODES 512
3394
3395 /** Compares the attributes of two Const nodes. */
3396 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3397         return (get_Const_tarval(a) != get_Const_tarval(b))
3398             || (get_Const_type(a) != get_Const_type(b));
3399 }  /* node_cmp_attr_Const */
3400
3401 /** Compares the attributes of two Proj nodes. */
3402 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3403         return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3404 }  /* node_cmp_attr_Proj */
3405
3406 /** Compares the attributes of two Filter nodes. */
3407 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3408         return get_Filter_proj(a) != get_Filter_proj(b);
3409 }  /* node_cmp_attr_Filter */
3410
3411 /** Compares the attributes of two Alloc nodes. */
3412 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3413         return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3414             || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3415 }  /* node_cmp_attr_Alloc */
3416
3417 /** Compares the attributes of two Free nodes. */
3418 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3419         return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3420             || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3421 }  /* node_cmp_attr_Free */
3422
3423 /** Compares the attributes of two SymConst nodes. */
3424 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3425         return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3426             || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3427             || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3428 }  /* node_cmp_attr_SymConst */
3429
3430 /** Compares the attributes of two Call nodes. */
3431 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3432         return (get_irn_call_attr(a) != get_irn_call_attr(b));
3433 }  /* node_cmp_attr_Call */
3434
3435 /** Compares the attributes of two Sel nodes. */
3436 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3437         return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
3438             || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
3439             || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
3440             || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3441             || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
3442 }  /* node_cmp_attr_Sel */
3443
3444 /** Compares the attributes of two Phi nodes. */
3445 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3446         return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3447 }  /* node_cmp_attr_Phi */
3448
3449 /** Compares the attributes of two Conv nodes. */
3450 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3451         return get_Conv_strict(a) != get_Conv_strict(b);
3452 }  /* node_cmp_attr_Conv */
3453
3454 /** Compares the attributes of two Cast nodes. */
3455 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3456         return get_Cast_type(a) != get_Cast_type(b);
3457 }  /* node_cmp_attr_Cast */
3458
3459 /** Compares the attributes of two Load nodes. */
3460 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3461         if (get_Load_volatility(a) == volatility_is_volatile ||
3462             get_Load_volatility(b) == volatility_is_volatile)
3463                 /* NEVER do CSE on volatile Loads */
3464                 return 1;
3465
3466         return get_Load_mode(a) != get_Load_mode(b);
3467 }  /* node_cmp_attr_Load */
3468
3469 /** Compares the attributes of two Store nodes. */
3470 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3471         /* NEVER do CSE on volatile Stores */
3472         return (get_Store_volatility(a) == volatility_is_volatile ||
3473                 get_Store_volatility(b) == volatility_is_volatile);
3474 }  /* node_cmp_attr_Store */
3475
3476 /** Compares the attributes of two Confirm nodes. */
3477 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3478         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3479 }  /* node_cmp_attr_Confirm */
3480
3481 /**
3482  * Set the default node attribute compare operation for an ir_op_ops.
3483  *
3484  * @param code   the opcode for the default operation
3485  * @param ops    the operations initialized
3486  *
3487  * @return
3488  *    The operations.
3489  */
3490 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3491 {
3492 #define CASE(a)                              \
3493         case iro_##a:                              \
3494                 ops->node_cmp_attr  = node_cmp_attr_##a; \
3495                 break
3496
3497         switch (code) {
3498         CASE(Const);
3499         CASE(Proj);
3500         CASE(Filter);
3501         CASE(Alloc);
3502         CASE(Free);
3503         CASE(SymConst);
3504         CASE(Call);
3505         CASE(Sel);
3506         CASE(Phi);
3507         CASE(Conv);
3508         CASE(Cast);
3509         CASE(Load);
3510         CASE(Store);
3511         CASE(Confirm);
3512         default:
3513           /* leave NULL */;
3514         }
3515
3516         return ops;
3517 #undef CASE
3518 }  /* firm_set_default_node_cmp_attr */
3519
3520 /*
3521  * Compare function for two nodes in the hash table. Gets two
3522  * nodes as parameters.  Returns 0 if the nodes are a cse.
3523  */
3524 int identities_cmp(const void *elt, const void *key) {
3525         ir_node *a, *b;
3526         int i, irn_arity_a;
3527
3528         a = (void *)elt;
3529         b = (void *)key;
3530
3531         if (a == b) return 0;
3532
3533         if ((get_irn_op(a) != get_irn_op(b)) ||
3534             (get_irn_mode(a) != get_irn_mode(b))) return 1;
3535
3536         /* compare if a's in and b's in are of equal length */
3537         irn_arity_a = get_irn_intra_arity (a);
3538         if (irn_arity_a != get_irn_intra_arity(b))
3539                 return 1;
3540
3541         /* for block-local cse and op_pin_state_pinned nodes: */
3542         if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3543                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3544                         return 1;
3545         }
3546
3547         /* compare a->in[0..ins] with b->in[0..ins] */
3548         for (i = 0; i < irn_arity_a; i++)
3549                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3550                         return 1;
3551
3552         /*
3553          * here, we already now that the nodes are identical except their
3554          * attributes
3555          */
3556         if (a->op->ops.node_cmp_attr)
3557                 return a->op->ops.node_cmp_attr(a, b);
3558
3559         return 0;
3560 }  /* identities_cmp */
3561
3562 /*
3563  * Calculate a hash value of a node.
3564  */
3565 unsigned ir_node_hash(ir_node *node) {
3566         unsigned h;
3567         int i, irn_arity;
3568
3569         if (node->op == op_Const) {
3570                 /* special value for const, as they only differ in their tarval. */
3571                 h = HASH_PTR(node->attr.con.tv);
3572                 h = 9*h + HASH_PTR(get_irn_mode(node));
3573         } else if (node->op == op_SymConst) {
3574                 /* special value for const, as they only differ in their symbol. */
3575                 h = HASH_PTR(node->attr.symc.sym.type_p);
3576                 h = 9*h + HASH_PTR(get_irn_mode(node));
3577         } else {
3578
3579                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3580                 h = irn_arity = get_irn_intra_arity(node);
3581
3582                 /* consider all in nodes... except the block if not a control flow. */
3583                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
3584                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3585                 }
3586
3587                 /* ...mode,... */
3588                 h = 9*h + HASH_PTR(get_irn_mode(node));
3589                 /* ...and code */
3590                 h = 9*h + HASH_PTR(get_irn_op(node));
3591         }
3592
3593         return h;
3594 }  /* ir_node_hash */
3595
3596 pset *new_identities(void) {
3597         return new_pset(identities_cmp, N_IR_NODES);
3598 }  /* new_identities */
3599
3600 void del_identities(pset *value_table) {
3601         del_pset(value_table);
3602 }  /* del_identities */
3603
3604 /**
3605  * Return the canonical node computing the same value as n.
3606  *
3607  * @param value_table  The value table
3608  * @param n            The node to lookup
3609  *
3610  * Looks up the node in a hash table.
3611  *
3612  * For Const nodes this is performed in the constructor, too.  Const
3613  * nodes are extremely time critical because of their frequent use in
3614  * constant string arrays.
3615  */
3616 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3617         ir_node *o = NULL;
3618
3619         if (!value_table) return n;
3620
3621         if (get_opt_reassociation()) {
3622                 if (is_op_commutative(get_irn_op(n))) {
3623                         ir_node *l = get_binop_left(n);
3624                         ir_node *r = get_binop_right(n);
3625
3626                         /* for commutative operators perform  a OP b == b OP a */
3627                         if (get_irn_idx(l) > get_irn_idx(r)) {
3628                                 set_binop_left(n, r);
3629                                 set_binop_right(n, l);
3630                         }
3631                 }
3632         }
3633
3634         o = pset_find(value_table, n, ir_node_hash(n));
3635         if (!o) return n;
3636
3637         DBG_OPT_CSE(n, o);
3638
3639         return o;
3640 }  /* identify */
3641
3642 /**
3643  * During construction we set the op_pin_state_pinned flag in the graph right when the
3644  * optimization is performed.  The flag turning on procedure global cse could
3645  * be changed between two allocations.  This way we are safe.
3646  */
3647 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3648         ir_node *old = n;
3649
3650         n = identify(value_table, n);
3651         if (get_irn_n(old, -1) != get_irn_n(n, -1))
3652                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3653         return n;
3654 }  /* identify_cons */
3655
3656 /*
3657  * Return the canonical node computing the same value as n.
3658  * Looks up the node in a hash table, enters it in the table
3659  * if it isn't there yet.
3660  */
3661 ir_node *identify_remember(pset *value_table, ir_node *n) {
3662         ir_node *o = NULL;
3663
3664         if (!value_table) return n;
3665
3666         if (get_opt_reassociation()) {
3667                 if (is_op_commutative(get_irn_op(n))) {
3668                         ir_node *l = get_binop_left(n);
3669                         ir_node *r = get_binop_right(n);
3670                         int l_idx = get_irn_idx(l);
3671                         int r_idx = get_irn_idx(r);
3672
3673                         /* For commutative operators perform  a OP b == b OP a but keep
3674                            constants on the RIGHT side. This helps greatly in some optimizations.
3675                            Moreover we use the idx number to make the form deterministic. */
3676                         if (is_irn_constlike(l))
3677                                 l_idx = -l_idx;
3678                         if (is_irn_constlike(r))
3679                                 r_idx = -r_idx;
3680                         if (l_idx < r_idx) {
3681                                 set_binop_left(n, r);
3682                                 set_binop_right(n, l);
3683                         }
3684                 }
3685         }
3686
3687         /* lookup or insert in hash table with given hash key. */
3688         o = pset_insert(value_table, n, ir_node_hash(n));
3689
3690         if (o != n) {
3691                 DBG_OPT_CSE(n, o);
3692         }
3693
3694         return o;
3695 }  /* identify_remember */
3696
3697 /* Add a node to the identities value table. */
3698 void add_identities(pset *value_table, ir_node *node) {
3699         if (get_opt_cse() && is_no_Block(node))
3700                 identify_remember(value_table, node);
3701 }  /* add_identities */
3702
3703 /* Visit each node in the value table of a graph. */
3704 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3705         ir_node *node;
3706         ir_graph *rem = current_ir_graph;
3707
3708         current_ir_graph = irg;
3709         foreach_pset(irg->value_table, node)
3710                 visit(node, env);
3711         current_ir_graph = rem;
3712 }  /* visit_all_identities */
3713
3714 /**
3715  * Garbage in, garbage out. If a node has a dead input, i.e., the
3716  * Bad node is input to the node, return the Bad node.
3717  */
3718 static INLINE ir_node *gigo(ir_node *node) {
3719         int i, irn_arity;
3720         ir_op *op = get_irn_op(node);
3721
3722         /* remove garbage blocks by looking at control flow that leaves the block
3723            and replacing the control flow by Bad. */
3724         if (get_irn_mode(node) == mode_X) {
3725                 ir_node *block = get_nodes_block(skip_Proj(node));
3726
3727                 /* Don't optimize nodes in immature blocks. */
3728                 if (!get_Block_matured(block)) return node;
3729                 /* Don't optimize End, may have Bads. */
3730                 if (op == op_End) return node;
3731
3732                 if (is_Block(block)) {
3733                         irn_arity = get_irn_arity(block);
3734                         for (i = 0; i < irn_arity; i++) {
3735                                 if (!is_Bad(get_irn_n(block, i)))
3736                                         break;
3737                         }
3738                         if (i == irn_arity) {
3739                                 ir_graph *irg = get_irn_irg(block);
3740                                 /* the start block is never dead */
3741                                 if (block != get_irg_start_block(irg)
3742                                         && block != get_irg_end_block(irg))
3743                                         return new_Bad();
3744                         }
3745                 }
3746         }
3747
3748         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3749            blocks predecessors is dead. */
3750         if (op != op_Block && op != op_Phi && op != op_Tuple) {
3751                 irn_arity = get_irn_arity(node);
3752
3753                 /*
3754                  * Beware: we can only read the block of a non-floating node.
3755                  */
3756                 if (is_irn_pinned_in_irg(node) &&
3757                         is_Block_dead(get_nodes_block(node)))
3758                         return new_Bad();
3759
3760                 for (i = 0; i < irn_arity; i++) {
3761                         ir_node *pred = get_irn_n(node, i);
3762
3763                         if (is_Bad(pred))
3764                                 return new_Bad();
3765 #if 0
3766                         /* Propagating Unknowns here seems to be a bad idea, because
3767                            sometimes we need a node as a input and did not want that
3768                            it kills it's user.
3769                            However, it might be useful to move this into a later phase
3770                            (if you think that optimizing such code is useful). */
3771                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3772                                 return new_Unknown(get_irn_mode(node));
3773 #endif
3774                 }
3775         }
3776 #if 0
3777         /* With this code we violate the agreement that local_optimize
3778            only leaves Bads in Block, Phi and Tuple nodes. */
3779         /* If Block has only Bads as predecessors it's garbage. */
3780         /* If Phi has only Bads as predecessors it's garbage. */
3781         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3782                 irn_arity = get_irn_arity(node);
3783                 for (i = 0; i < irn_arity; i++) {
3784                         if (!is_Bad(get_irn_n(node, i))) break;
3785                 }
3786                 if (i == irn_arity) node = new_Bad();
3787         }
3788 #endif
3789         return node;
3790 }  /* gigo */
3791
3792 /**
3793  * These optimizations deallocate nodes from the obstack.
3794  * It can only be called if it is guaranteed that no other nodes
3795  * reference this one, i.e., right after construction of a node.
3796  *
3797  * @param n   The node to optimize
3798  *
3799  * current_ir_graph must be set to the graph of the node!
3800  */
3801 ir_node *optimize_node(ir_node *n) {
3802         tarval *tv;
3803         ir_node *oldn = n;
3804         ir_opcode iro = get_irn_opcode(n);
3805
3806         /* Always optimize Phi nodes: part of the construction. */
3807         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3808
3809         /* constant expression evaluation / constant folding */
3810         if (get_opt_constant_folding()) {
3811                 /* neither constants nor Tuple values can be evaluated */
3812                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3813                         /* try to evaluate */
3814                         tv = computed_value(n);
3815                         if (tv != tarval_bad) {
3816                                 ir_node *nw;
3817                                 ir_type *old_tp = get_irn_type(n);
3818                                 int i, arity = get_irn_arity(n);
3819                                 int node_size;
3820
3821                                 /*
3822                                  * Try to recover the type of the new expression.
3823                                  */
3824                                 for (i = 0; i < arity && !old_tp; ++i)
3825                                         old_tp = get_irn_type(get_irn_n(n, i));
3826
3827                                 /*
3828                                  * we MUST copy the node here temporary, because it's still needed
3829                                  * for DBG_OPT_CSTEVAL
3830                                  */
3831                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3832                                 oldn = alloca(node_size);
3833
3834                                 memcpy(oldn, n, node_size);
3835                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3836
3837                                 /* ARG, copy the in array, we need it for statistics */
3838                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3839
3840                                 /* note the inplace edges module */
3841                                 edges_node_deleted(n, current_ir_graph);
3842
3843                                 /* evaluation was successful -- replace the node. */
3844                                 irg_kill_node(current_ir_graph, n);
3845                                 nw = new_Const(get_tarval_mode (tv), tv);
3846
3847                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3848                                         set_Const_type(nw, old_tp);
3849                                 DBG_OPT_CSTEVAL(oldn, nw);
3850                                 return nw;
3851                         }
3852                 }
3853         }
3854
3855         /* remove unnecessary nodes */
3856         if (get_opt_constant_folding() ||
3857             (iro == iro_Phi)  ||   /* always optimize these nodes. */
3858             (iro == iro_Id)   ||
3859             (iro == iro_Proj) ||
3860             (iro == iro_Block)  )  /* Flags tested local. */
3861                 n = equivalent_node(n);
3862
3863         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3864
3865         /* Common Subexpression Elimination.
3866          *
3867          * Checks whether n is already available.
3868          * The block input is used to distinguish different subexpressions. Right
3869          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3870          * subexpressions within a block.
3871          */
3872         if (get_opt_cse())
3873                 n = identify_cons(current_ir_graph->value_table, n);
3874
3875         if (n != oldn) {
3876                 edges_node_deleted(oldn, current_ir_graph);
3877
3878                 /* We found an existing, better node, so we can deallocate the old node. */
3879                 irg_kill_node(current_ir_graph, oldn);
3880                 return n;
3881         }
3882
3883         /* Some more constant expression evaluation that does not allow to
3884            free the node. */
3885         iro = get_irn_opcode(n);
3886         if (get_opt_constant_folding() ||
3887             (iro == iro_Cond) ||
3888             (iro == iro_Proj))     /* Flags tested local. */
3889                 n = transform_node(n);
3890
3891         /* Remove nodes with dead (Bad) input.
3892            Run always for transformation induced Bads. */
3893         n = gigo (n);
3894
3895         /* Now we have a legal, useful node. Enter it in hash table for CSE */
3896         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3897                 n = identify_remember(current_ir_graph->value_table, n);
3898         }
3899
3900         return n;
3901 }  /* optimize_node */
3902
3903
3904 /**
3905  * These optimizations never deallocate nodes (in place).  This can cause dead
3906  * nodes lying on the obstack.  Remove these by a dead node elimination,
3907  * i.e., a copying garbage collection.
3908  */
3909 ir_node *optimize_in_place_2(ir_node *n) {
3910         tarval *tv;
3911         ir_node *oldn = n;
3912         ir_opcode iro = get_irn_opcode(n);
3913
3914         if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3915
3916         /* constant expression evaluation / constant folding */
3917         if (get_opt_constant_folding()) {
3918                 /* neither constants nor Tuple values can be evaluated */
3919                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3920                         /* try to evaluate */
3921                         tv = computed_value(n);
3922                         if (tv != tarval_bad) {
3923                                 /* evaluation was successful -- replace the node. */
3924                                 ir_type *old_tp = get_irn_type(n);
3925                                 int i, arity = get_irn_arity(n);
3926
3927                                 /*
3928                                  * Try to recover the type of the new expression.
3929                                  */
3930                                 for (i = 0; i < arity && !old_tp; ++i)
3931                                         old_tp = get_irn_type(get_irn_n(n, i));
3932
3933                                 n = new_Const(get_tarval_mode(tv), tv);
3934
3935                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3936                                         set_Const_type(n, old_tp);
3937
3938                                 DBG_OPT_CSTEVAL(oldn, n);
3939                                 return n;
3940                         }
3941                 }
3942         }
3943
3944         /* remove unnecessary nodes */
3945         if (get_opt_constant_folding() ||
3946             (iro == iro_Phi)  ||   /* always optimize these nodes. */
3947             (iro == iro_Id)   ||   /* ... */
3948             (iro == iro_Proj) ||   /* ... */
3949             (iro == iro_Block)  )  /* Flags tested local. */
3950                 n = equivalent_node(n);
3951
3952         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3953
3954         /** common subexpression elimination **/
3955         /* Checks whether n is already available. */
3956         /* The block input is used to distinguish different subexpressions.  Right
3957            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3958            subexpressions within a block. */
3959         if (get_opt_cse()) {
3960                 n = identify(current_ir_graph->value_table, n);
3961         }
3962
3963         /* Some more constant expression evaluation. */
3964         iro = get_irn_opcode(n);
3965         if (get_opt_constant_folding() ||
3966                 (iro == iro_Cond) ||
3967                 (iro == iro_Proj))     /* Flags tested local. */
3968                 n = transform_node(n);
3969
3970         /* Remove nodes with dead (Bad) input.
3971            Run always for transformation induced Bads.  */
3972         n = gigo(n);
3973
3974         /* Now we can verify the node, as it has no dead inputs any more. */
3975         irn_vrfy(n);
3976
3977         /* Now we have a legal, useful node. Enter it in hash table for cse.
3978            Blocks should be unique anyways.  (Except the successor of start:
3979            is cse with the start block!) */
3980         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3981                 n = identify_remember(current_ir_graph->value_table, n);
3982
3983         return n;
3984 }  /* optimize_in_place_2 */
3985
3986 /**
3987  * Wrapper for external use, set proper status bits after optimization.
3988  */
3989 ir_node *optimize_in_place(ir_node *n) {
3990         /* Handle graph state */
3991         assert(get_irg_phase_state(current_ir_graph) != phase_building);
3992
3993         if (get_opt_global_cse())
3994                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3995         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3996                 set_irg_outs_inconsistent(current_ir_graph);
3997
3998         /* FIXME: Maybe we could also test whether optimizing the node can
3999            change the control graph. */
4000         set_irg_doms_inconsistent(current_ir_graph);
4001         return optimize_in_place_2(n);
4002 }  /* optimize_in_place */
4003
4004 /*
4005  * Sets the default operation for an ir_ops.
4006  */
4007 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4008         ops = firm_set_default_computed_value(code, ops);
4009         ops = firm_set_default_equivalent_node(code, ops);
4010         ops = firm_set_default_transform_node(code, ops);
4011         ops = firm_set_default_node_cmp_attr(code, ops);
4012         ops = firm_set_default_get_type(code, ops);
4013         ops = firm_set_default_get_type_attr(code, ops);
4014         ops = firm_set_default_get_entity_attr(code, ops);
4015
4016         return ops;
4017 }  /* firm_set_default_operations */