Add constraints to the ASM node
[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                 ir_node *blk = get_nodes_block(n);
1055                 turn_into_tuple(n, pn_Div_max);
1056                 set_Tuple_pred(n, pn_Div_M,         mem);
1057                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1058                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());        /* no exception */
1059                 set_Tuple_pred(n, pn_Div_res,       a);
1060         }
1061         return n;
1062 }  /* equivalent_node_Div */
1063
1064 /**
1065  * Optimize a / 1.0 = a.
1066  */
1067 static ir_node *equivalent_node_Quot(ir_node *n) {
1068         ir_node *a = get_Quot_left(n);
1069         ir_node *b = get_Quot_right(n);
1070
1071         /* Div is not commutative. */
1072         if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1073                 /* Turn Quot into a tuple (mem, bad, a) */
1074                 ir_node *mem = get_Quot_mem(n);
1075                 ir_node *blk = get_nodes_block(n);
1076                 turn_into_tuple(n, pn_Quot_max);
1077                 set_Tuple_pred(n, pn_Quot_M,         mem);
1078                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1079                 set_Tuple_pred(n, pn_Quot_X_except,  new_Bad());        /* no exception */
1080                 set_Tuple_pred(n, pn_Quot_res,       a);
1081         }
1082         return n;
1083 }  /* equivalent_node_Quot */
1084
1085 /**
1086  * Optimize a / 1 = a.
1087  */
1088 static ir_node *equivalent_node_DivMod(ir_node *n) {
1089         ir_node *b = get_DivMod_right(n);
1090
1091         /* Div is not commutative. */
1092         if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1093                 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1094                 ir_node *a = get_DivMod_left(n);
1095                 ir_node *mem = get_Div_mem(n);
1096                 ir_node *blk = get_nodes_block(n);
1097                 ir_mode *mode = get_DivMod_resmode(n);
1098
1099                 turn_into_tuple(n, pn_DivMod_max);
1100                 set_Tuple_pred(n, pn_DivMod_M,         mem);
1101                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1102                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());        /* no exception */
1103                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
1104                 set_Tuple_pred(n, pn_DivMod_res_mod,   new_Const(mode, get_mode_null(mode)));
1105         }
1106         return n;
1107 }  /* equivalent_node_DivMod */
1108
1109 /**
1110  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1111  */
1112 static ir_node *equivalent_node_Or(ir_node *n) {
1113         ir_node *oldn = n;
1114
1115         ir_node *a = get_Or_left(n);
1116         ir_node *b = get_Or_right(n);
1117
1118         if (a == b) {
1119                 n = a;    /* Or has it's own neutral element */
1120                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1121         } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1122                 n = b;
1123                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1124         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1125                 n = a;
1126                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1127         }
1128
1129         return n;
1130 }  /* equivalent_node_Or */
1131
1132 /**
1133  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
1134  */
1135 static ir_node *equivalent_node_And(ir_node *n) {
1136         ir_node *oldn = n;
1137
1138         ir_node *a = get_And_left(n);
1139         ir_node *b = get_And_right(n);
1140
1141         if (a == b) {
1142                 n = a;    /* And has it's own neutral element */
1143                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1144         } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1145                 n = b;
1146                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1147         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1148                 n = a;
1149                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1150         }
1151         return n;
1152 }  /* equivalent_node_And */
1153
1154 /**
1155  * Try to remove useless Conv's:
1156  */
1157 static ir_node *equivalent_node_Conv(ir_node *n) {
1158         ir_node *oldn = n;
1159         ir_node *a = get_Conv_op(n);
1160         ir_node *b;
1161
1162         ir_mode *n_mode = get_irn_mode(n);
1163         ir_mode *a_mode = get_irn_mode(a);
1164
1165         if (n_mode == a_mode) { /* No Conv necessary */
1166                 /* leave strict floating point Conv's */
1167                 if (get_Conv_strict(n))
1168                         return n;
1169                 n = a;
1170                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1171         } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1172                 ir_mode *b_mode;
1173
1174                 b = get_Conv_op(a);
1175                 n_mode = get_irn_mode(n);
1176                 b_mode = get_irn_mode(b);
1177
1178                 if (n_mode == b_mode) {
1179                         if (n_mode == mode_b) {
1180                                 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1181                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1182                         } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1183                                 if (smaller_mode(b_mode, a_mode)){
1184                                         n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1185                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1186                                 }
1187                         }
1188                 }
1189         }
1190         return n;
1191 }  /* equivalent_node_Conv */
1192
1193 /**
1194  * A Cast may be removed if the type of the previous node
1195  * is already the type of the Cast.
1196  */
1197 static ir_node *equivalent_node_Cast(ir_node *n) {
1198         ir_node *oldn = n;
1199         ir_node *pred = get_Cast_op(n);
1200
1201         if (get_irn_type(pred) == get_Cast_type(n)) {
1202                 n = pred;
1203                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1204         }
1205         return n;
1206 }  /* equivalent_node_Cast */
1207
1208 /**
1209  * Several optimizations:
1210  * - no Phi in start block.
1211  * - remove Id operators that are inputs to Phi
1212  * - fold Phi-nodes, iff they have only one predecessor except
1213  *   themselves.
1214  */
1215 static ir_node *equivalent_node_Phi(ir_node *n) {
1216         int i, n_preds;
1217
1218         ir_node *oldn = n;
1219         ir_node *block = NULL;     /* to shutup gcc */
1220         ir_node *first_val = NULL; /* to shutup gcc */
1221
1222         if (!get_opt_normalize()) return n;
1223
1224         n_preds = get_Phi_n_preds(n);
1225
1226         block = get_nodes_block(n);
1227         /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1228         assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1229         if ((is_Block_dead(block)) ||                  /* Control dead */
1230                 (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1231                 return new_Bad();                                    /* in the Start Block. */
1232
1233         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1234
1235         /* If the Block has a Bad pred, we also have one. */
1236         for (i = 0;  i < n_preds;  ++i)
1237                 if (is_Bad(get_Block_cfgpred(block, i)))
1238                         set_Phi_pred(n, i, new_Bad());
1239
1240         /* Find first non-self-referencing input */
1241         for (i = 0; i < n_preds; ++i) {
1242                 first_val = get_Phi_pred(n, i);
1243                 if (   (first_val != n)                            /* not self pointer */
1244 #if 1
1245                     && (! is_Bad(first_val))
1246 #endif
1247                    ) {        /* value not dead */
1248                         break;          /* then found first value. */
1249                 }
1250         }
1251
1252         if (i >= n_preds) {
1253                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1254                 return new_Bad();
1255         }
1256
1257         /* search for rest of inputs, determine if any of these
1258         are non-self-referencing */
1259         while (++i < n_preds) {
1260                 ir_node *scnd_val = get_Phi_pred(n, i);
1261                 if (   (scnd_val != n)
1262                     && (scnd_val != first_val)
1263 #if 1
1264                     && (! is_Bad(scnd_val))
1265 #endif
1266                         ) {
1267                         break;
1268                 }
1269         }
1270
1271         if (i >= n_preds) {
1272                 /* Fold, if no multiple distinct non-self-referencing inputs */
1273                 n = first_val;
1274                 DBG_OPT_PHI(oldn, n);
1275         }
1276         return n;
1277 }  /* equivalent_node_Phi */
1278
1279 /**
1280  * Several optimizations:
1281  * - no Sync in start block.
1282  * - fold Sync-nodes, iff they have only one predecessor except
1283  *   themselves.
1284  */
1285 static ir_node *equivalent_node_Sync(ir_node *n) {
1286         int i, n_preds;
1287
1288         ir_node *oldn = n;
1289         ir_node *first_val = NULL; /* to shutup gcc */
1290
1291         if (!get_opt_normalize()) return n;
1292
1293         n_preds = get_Sync_n_preds(n);
1294
1295         /* Find first non-self-referencing input */
1296         for (i = 0; i < n_preds; ++i) {
1297                 first_val = get_Sync_pred(n, i);
1298                 if ((first_val != n)  /* not self pointer */ &&
1299                     (! is_Bad(first_val))
1300                    ) {                /* value not dead */
1301                         break;            /* then found first value. */
1302                 }
1303         }
1304
1305         if (i >= n_preds)
1306                 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1307                 return new_Bad();
1308
1309         /* search the rest of inputs, determine if any of these
1310            are non-self-referencing */
1311         while (++i < n_preds) {
1312                 ir_node *scnd_val = get_Sync_pred(n, i);
1313                 if ((scnd_val != n) &&
1314                     (scnd_val != first_val) &&
1315                     (! is_Bad(scnd_val))
1316                    )
1317                         break;
1318         }
1319
1320         if (i >= n_preds) {
1321                 /* Fold, if no multiple distinct non-self-referencing inputs */
1322                 n = first_val;
1323                 DBG_OPT_SYNC(oldn, n);
1324         }
1325         return n;
1326 }  /* equivalent_node_Sync */
1327
1328 /**
1329  * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1330  * ProjX(Load) and ProjX(Store).
1331  */
1332 static ir_node *equivalent_node_Proj(ir_node *n) {
1333         ir_node *oldn = n;
1334         ir_node *a = get_Proj_pred(n);
1335
1336         if ( get_irn_op(a) == op_Tuple) {
1337                 /* Remove the Tuple/Proj combination. */
1338                 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1339                         n = get_Tuple_pred(a, get_Proj_proj(n));
1340                         DBG_OPT_TUPLE(oldn, a, n);
1341                 } else {
1342                         assert(0); /* This should not happen! */
1343                         n = new_Bad();
1344                 }
1345         } else if (get_irn_mode(n) == mode_X) {
1346                 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1347                         /* Remove dead control flow -- early gigo(). */
1348                         n = new_Bad();
1349                 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1350                         ir_op *op = get_irn_op(a);
1351
1352                         if (op == op_Load || op == op_Store) {
1353                                 /* get the load/store address */
1354                                 ir_node *addr = get_irn_n(a, 1);
1355                                 ir_node *confirm;
1356
1357                                 if (value_not_null(addr, &confirm)) {
1358                                         if (confirm == NULL) {
1359                                                 /* this node may float if it did not depend on a Confirm */
1360                                                 set_irn_pinned(a, op_pin_state_floats);
1361                                         }
1362                                         DBG_OPT_EXC_REM(n);
1363                                         return new_Bad();
1364                                 }
1365                         }
1366                 }
1367         }
1368
1369         return n;
1370 }  /* equivalent_node_Proj */
1371
1372 /**
1373  * Remove Id's.
1374  */
1375 static ir_node *equivalent_node_Id(ir_node *n) {
1376         ir_node *oldn = n;
1377
1378         do {
1379                 n = get_Id_pred(n);
1380         } while (get_irn_op(n) == op_Id);
1381
1382         DBG_OPT_ID(oldn, n);
1383         return n;
1384 }  /* equivalent_node_Id */
1385
1386 /**
1387  * Optimize a Mux.
1388  */
1389 static ir_node *equivalent_node_Mux(ir_node *n)
1390 {
1391         ir_node *oldn = n, *sel = get_Mux_sel(n);
1392         tarval *ts = value_of(sel);
1393
1394         /* Mux(true, f, t) == t */
1395         if (ts == tarval_b_true) {
1396                 n = get_Mux_true(n);
1397                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1398         }
1399         /* Mux(false, f, t) == f */
1400         else if (ts == tarval_b_false) {
1401                 n = get_Mux_false(n);
1402                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1403         }
1404         /* Mux(v, x, x) == x */
1405         else if (get_Mux_false(n) == get_Mux_true(n)) {
1406                 n = get_Mux_true(n);
1407                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1408         }
1409         else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1410                 ir_node *cmp = get_Proj_pred(sel);
1411                 long proj_nr = get_Proj_proj(sel);
1412                 ir_node *b   = get_Mux_false(n);
1413                 ir_node *a   = get_Mux_true(n);
1414
1415                 /*
1416                  * Note: normalization puts the constant on the right site,
1417                  * so we check only one case.
1418                  *
1419                  * Note further that these optimization work even for floating point
1420                  * with NaN's because -NaN == NaN.
1421                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1422                  */
1423                 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1424                         if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1425                                 /* Mux(a CMP 0, X, a) */
1426                                 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1427                                         /* Mux(a CMP 0, -a, a) */
1428                                         if (proj_nr == pn_Cmp_Eq) {
1429                                                 /* Mux(a == 0, -a, a)  ==>  -a */
1430                                                 n = b;
1431                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1432                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1433                                                 /* Mux(a != 0, -a, a)  ==> a */
1434                                                 n = a;
1435                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1436                                         }
1437                                 } else if (classify_Const(b) == CNST_NULL) {
1438                                         /* Mux(a CMP 0, 0, a) */
1439                                         if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1440                                                 /* Mux(a != 0, 0, a) ==> a */
1441                                                 n = a;
1442                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1443                                         } else if (proj_nr == pn_Cmp_Eq) {
1444                                                 /* Mux(a == 0, 0, a) ==> 0 */
1445                                                 n = b;
1446                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1447                                         }
1448                                 }
1449                         }
1450                 }
1451         }
1452         return n;
1453 }  /* equivalent_node_Mux */
1454
1455 /**
1456  * Returns a equivalent node of a Psi: if a condition is true
1457  * and all previous conditions are false we know its value.
1458  * If all conditions are false its value is the default one.
1459  */
1460 static ir_node *equivalent_node_Psi(ir_node *n) {
1461         if (is_Mux(n))
1462                 return equivalent_node_Mux(n);
1463         return n;
1464 }  /* equivalent_node_Psi */
1465
1466 /**
1467  * Optimize -a CMP -b into b CMP a.
1468  * This works only for for modes where unary Minus
1469  * cannot Overflow.
1470  * Note that two-complement integers can Overflow
1471  * so it will NOT work.
1472  *
1473  * For == and != can be handled in Proj(Cmp)
1474  */
1475 static ir_node *equivalent_node_Cmp(ir_node *n) {
1476         ir_node *left  = get_Cmp_left(n);
1477         ir_node *right = get_Cmp_right(n);
1478
1479         if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1480                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1481                 left  = get_Minus_op(left);
1482                 right = get_Minus_op(right);
1483                 set_Cmp_left(n, right);
1484                 set_Cmp_right(n, left);
1485         }
1486         return n;
1487 }  /* equivalent_node_Cmp */
1488
1489 /**
1490  * Remove Confirm nodes if setting is on.
1491  * Replace Confirms(x, '=', Constlike) by Constlike.
1492  */
1493 static ir_node *equivalent_node_Confirm(ir_node *n) {
1494         ir_node *pred = get_Confirm_value(n);
1495         pn_Cmp  pnc   = get_Confirm_cmp(n);
1496
1497         if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1498                 /*
1499                  * rare case: two identical Confirms one after another,
1500                  * replace the second one with the first.
1501                  */
1502                 n = pred;
1503         }
1504         if (pnc == pn_Cmp_Eq) {
1505                 ir_node *bound = get_Confirm_bound(n);
1506
1507                 /*
1508                  * Optimize a rare case:
1509                  * Confirm(x, '=', Constlike) ==> Constlike
1510                  */
1511                 if (is_irn_constlike(bound)) {
1512                         DBG_OPT_CONFIRM(n, bound);
1513                         return bound;
1514                 }
1515         }
1516         return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1517 }
1518
1519 /**
1520  * Optimize CopyB(mem, x, x) into a Nop.
1521  */
1522 static ir_node *equivalent_node_CopyB(ir_node *n) {
1523         ir_node *a = get_CopyB_dst(n);
1524         ir_node *b = get_CopyB_src(n);
1525
1526         if (a == b) {
1527                 /* Turn CopyB into a tuple (mem, bad, bad) */
1528                 ir_node *mem = get_CopyB_mem(n);
1529                 ir_node *blk = get_nodes_block(n);
1530                 turn_into_tuple(n, pn_CopyB_max);
1531                 set_Tuple_pred(n, pn_CopyB_M,        mem);
1532                 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1533                 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad());        /* no exception */
1534                 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1535         }
1536         return n;
1537 }  /* equivalent_node_CopyB */
1538
1539 /**
1540  * Optimize Bounds(idx, idx, upper) into idx.
1541  */
1542 static ir_node *equivalent_node_Bound(ir_node *n) {
1543         ir_node *idx   = get_Bound_index(n);
1544         ir_node *lower = get_Bound_lower(n);
1545         int ret_tuple = 0;
1546
1547         /* By definition lower < upper, so if idx == lower -->
1548         lower <= idx && idx < upper */
1549         if (idx == lower) {
1550                 /* Turn Bound into a tuple (mem, bad, idx) */
1551                 ret_tuple = 1;
1552         } else {
1553                 ir_node *pred = skip_Proj(idx);
1554
1555                 if (get_irn_op(pred) == op_Bound) {
1556                         /*
1557                          * idx was Bounds_check previously, it is still valid if
1558                          * lower <= pred_lower && pred_upper <= upper.
1559                          */
1560                         ir_node *upper = get_Bound_upper(n);
1561                         if (get_Bound_lower(pred) == lower &&
1562                                 get_Bound_upper(pred) == upper) {
1563                                 /*
1564                                  * One could expect that we simply return the previous
1565                                  * Bound here. However, this would be wrong, as we could
1566                                  * add an exception Proj to a new location than.
1567                                  * So, we must turn in into a tuple
1568                                  */
1569                                 ret_tuple = 1;
1570                         }
1571                 }
1572         }
1573         if (ret_tuple) {
1574                 /* Turn Bound into a tuple (mem, bad, idx) */
1575                 ir_node *mem = get_Bound_mem(n);
1576                 ir_node *blk = get_nodes_block(n);
1577                 turn_into_tuple(n, pn_Bound_max);
1578                 set_Tuple_pred(n, pn_Bound_M,         mem);
1579                 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk));       /* no exception */
1580                 set_Tuple_pred(n, pn_Bound_X_except,  new_Bad());       /* no exception */
1581                 set_Tuple_pred(n, pn_Bound_res,       idx);
1582         }
1583         return n;
1584 }  /* equivalent_node_Bound */
1585
1586 /**
1587  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1588  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1589  * new nodes.  It is therefore safe to free n if the node returned is not n.
1590  * If a node returns a Tuple we can not just skip it.  If the size of the
1591  * in array fits, we transform n into a tuple (e.g., Div).
1592  */
1593 ir_node *equivalent_node(ir_node *n) {
1594         if (n->op->ops.equivalent_node)
1595                 return n->op->ops.equivalent_node(n);
1596         return n;
1597 }  /* equivalent_node */
1598
1599 /**
1600  * Sets the default equivalent node operation for an ir_op_ops.
1601  *
1602  * @param code   the opcode for the default operation
1603  * @param ops    the operations initialized
1604  *
1605  * @return
1606  *    The operations.
1607  */
1608 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1609 {
1610 #define CASE(a)                                      \
1611         case iro_##a:                                    \
1612                 ops->equivalent_node  = equivalent_node_##a; \
1613                 break
1614
1615         switch (code) {
1616         CASE(Block);
1617         CASE(Jmp);
1618         CASE(Raise);
1619         CASE(Or);
1620         CASE(Add);
1621         CASE(Eor);
1622         CASE(Sub);
1623         CASE(Shl);
1624         CASE(Shr);
1625         CASE(Shrs);
1626         CASE(Rot);
1627         CASE(Not);
1628         CASE(Minus);
1629         CASE(Mul);
1630         CASE(Div);
1631         CASE(Quot);
1632         CASE(DivMod);
1633         CASE(And);
1634         CASE(Conv);
1635         CASE(Cast);
1636         CASE(Phi);
1637         CASE(Sync);
1638         CASE(Proj);
1639         CASE(Id);
1640         CASE(Mux);
1641         CASE(Psi);
1642         CASE(Cmp);
1643         CASE(Confirm);
1644         CASE(CopyB);
1645         CASE(Bound);
1646         default:
1647                 /* leave NULL */;
1648         }
1649
1650         return ops;
1651 #undef CASE
1652 }  /* firm_set_default_equivalent_node */
1653
1654 /**
1655  * Do node specific optimizations of nodes predecessors.
1656  */
1657 static void optimize_preds(ir_node *n) {
1658         switch (get_irn_opcode(n)) {
1659
1660         case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1661                 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1662
1663                 if (get_irn_op(a) == op_Cast) {
1664                         a = get_Cast_op(a);
1665                         set_Cmp_left(n, a);
1666                 }
1667                 if (get_irn_op(b) == op_Cast) {
1668                         b = get_Cast_op(b);
1669                         set_Cmp_right(n, b);
1670                 }
1671                 break;
1672         }
1673
1674         default: break;
1675         } /* end switch */
1676 }  /* optimize_preds */
1677
1678 /**
1679  * Returns non-zero if a node is a Phi node
1680  * with all predecessors constant.
1681  */
1682 static int is_const_Phi(ir_node *n) {
1683         int i;
1684
1685         if (! is_Phi(n))
1686                 return 0;
1687         for (i = get_irn_arity(n) - 1; i >= 0; --i)
1688                 if (! is_Const(get_irn_n(n, i)))
1689                         return 0;
1690                 return 1;
1691 }  /* is_const_Phi */
1692
1693 /**
1694  * Apply an evaluator on a binop with a constant operators (and one Phi).
1695  *
1696  * @param phi    the Phi node
1697  * @param other  the other operand
1698  * @param eval   an evaluator function
1699  * @param left   if non-zero, other is the left operand, else the right
1700  *
1701  * @return a new Phi node if the conversion was successful, NULL else
1702  */
1703 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1704         tarval   *tv;
1705         void     **res;
1706         ir_node  *pred;
1707         ir_mode  *mode;
1708         ir_graph *irg;
1709         int      i, n = get_irn_arity(phi);
1710
1711         NEW_ARR_A(void *, res, n);
1712         if (left) {
1713                 for (i = 0; i < n; ++i) {
1714                         pred = get_irn_n(phi, i);
1715                         tv   = get_Const_tarval(pred);
1716                         tv   = eval(other, tv);
1717
1718                         if (tv == tarval_bad) {
1719                                 /* folding failed, bad */
1720                                 return NULL;
1721                         }
1722                         res[i] = tv;
1723                 }
1724         } else {
1725                 for (i = 0; i < n; ++i) {
1726                         pred = get_irn_n(phi, i);
1727                         tv   = get_Const_tarval(pred);
1728                         tv   = eval(tv, other);
1729
1730                         if (tv == tarval_bad) {
1731                                 /* folding failed, bad */
1732                                 return 0;
1733                         }
1734                         res[i] = tv;
1735                 }
1736         }
1737         mode = get_irn_mode(phi);
1738         irg  = current_ir_graph;
1739         for (i = 0; i < n; ++i) {
1740                 pred = get_irn_n(phi, i);
1741                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1742                         mode, res[i], get_Const_type(pred));
1743         }
1744         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1745 }  /* apply_binop_on_phi */
1746
1747 /**
1748  * Apply an evaluator on a unop with a constant operator (a Phi).
1749  *
1750  * @param phi    the Phi node
1751  * @param eval   an evaluator function
1752  *
1753  * @return a new Phi node if the conversion was successful, NULL else
1754  */
1755 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1756         tarval   *tv;
1757         void     **res;
1758         ir_node  *pred;
1759         ir_mode  *mode;
1760         ir_graph *irg;
1761         int      i, n = get_irn_arity(phi);
1762
1763         NEW_ARR_A(void *, res, n);
1764         for (i = 0; i < n; ++i) {
1765                 pred = get_irn_n(phi, i);
1766                 tv   = get_Const_tarval(pred);
1767                 tv   = eval(tv);
1768
1769                 if (tv == tarval_bad) {
1770                         /* folding failed, bad */
1771                         return 0;
1772                 }
1773                 res[i] = tv;
1774         }
1775         mode = get_irn_mode(phi);
1776         irg  = current_ir_graph;
1777         for (i = 0; i < n; ++i) {
1778                 pred = get_irn_n(phi, i);
1779                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1780                         mode, res[i], get_Const_type(pred));
1781         }
1782         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1783 }  /* apply_unop_on_phi */
1784
1785 /**
1786  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1787  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1788  * If possible, remove the Conv's.
1789  */
1790 static ir_node *transform_node_AddSub(ir_node *n) {
1791         ir_mode *mode = get_irn_mode(n);
1792
1793         if (mode_is_reference(mode)) {
1794                 ir_node *left  = get_binop_left(n);
1795                 ir_node *right = get_binop_right(n);
1796                 int ref_bits   = get_mode_size_bits(mode);
1797
1798                 if (get_irn_op(left) == op_Conv) {
1799                         ir_mode *mode = get_irn_mode(left);
1800                         int bits      = get_mode_size_bits(mode);
1801
1802                         if (ref_bits == bits &&
1803                             mode_is_int(mode) &&
1804                             get_mode_arithmetic(mode) == irma_twos_complement) {
1805                                 ir_node *pre      = get_Conv_op(left);
1806                                 ir_mode *pre_mode = get_irn_mode(pre);
1807
1808                                 if (mode_is_int(pre_mode) &&
1809                                     get_mode_size_bits(pre_mode) == bits &&
1810                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1811                                         /* ok, this conv just changes to sign, moreover the calculation
1812                                          * is done with same number of bits as our address mode, so
1813                                          * we can ignore the conv as address calculation can be viewed
1814                                          * as either signed or unsigned
1815                                          */
1816                                         set_binop_left(n, pre);
1817                                 }
1818                         }
1819                 }
1820
1821                 if (get_irn_op(right) == op_Conv) {
1822                         ir_mode *mode = get_irn_mode(right);
1823                         int bits      = get_mode_size_bits(mode);
1824
1825                         if (ref_bits == bits &&
1826                                 mode_is_int(mode) &&
1827                                 get_mode_arithmetic(mode) == irma_twos_complement) {
1828                                 ir_node *pre      = get_Conv_op(right);
1829                                 ir_mode *pre_mode = get_irn_mode(pre);
1830
1831                                 if (mode_is_int(pre_mode) &&
1832                                     get_mode_size_bits(pre_mode) == bits &&
1833                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1834                                         /* ok, this conv just changes to sign, moreover the calculation
1835                                          * is done with same number of bits as our address mode, so
1836                                          * we can ignore the conv as address calculation can be viewed
1837                                          * as either signed or unsigned
1838                                          */
1839                                         set_binop_right(n, pre);
1840                                 }
1841                         }
1842                 }
1843         }
1844         return n;
1845 }  /* transform_node_AddSub */
1846
1847 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1848   c = NULL;                                                 \
1849   if (is_Const(b) && is_const_Phi(a)) {                     \
1850     /* check for Op(Phi, Const) */                          \
1851     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1852   }                                                         \
1853   else if (is_Const(a) && is_const_Phi(b)) {                \
1854     /* check for Op(Const, Phi) */                          \
1855     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1856   }                                                         \
1857   if (c) {                                                  \
1858     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1859     return c;                                               \
1860   }
1861
1862 #define HANDLE_UNOP_PHI(op,a,c)                 \
1863   c = NULL;                                     \
1864   if (is_const_Phi(a)) {                        \
1865     /* check for Op(Phi) */                     \
1866     c = apply_unop_on_phi(a, op);               \
1867   }                                             \
1868   if (c) {                                      \
1869     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1870     return c;                                   \
1871   }
1872
1873
1874 /**
1875  * Do the AddSub optimization, then Transform
1876  *   Constant folding on Phi
1877  *   Add(a,a)          -> Mul(a, 2)
1878  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1879  * if the mode is integer or float.
1880  * Transform Add(a,-b) into Sub(a,b).
1881  * Reassociation might fold this further.
1882  */
1883 static ir_node *transform_node_Add(ir_node *n) {
1884         ir_mode *mode;
1885         ir_node *a, *b, *c, *oldn = n;
1886
1887         n = transform_node_AddSub(n);
1888
1889         a = get_Add_left(n);
1890         b = get_Add_right(n);
1891
1892         HANDLE_BINOP_PHI(tarval_add, a,b,c);
1893
1894         mode = get_irn_mode(n);
1895
1896         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1897         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1898                 return n;
1899
1900         if (mode_is_num(mode)) {
1901                 if (a == b) {
1902                         ir_node *block = get_irn_n(n, -1);
1903
1904                         n = new_rd_Mul(
1905                                 get_irn_dbg_info(n),
1906                                 current_ir_graph,
1907                                 block,
1908                                 a,
1909                                 new_r_Const_long(current_ir_graph, block, mode, 2),
1910                                 mode);
1911                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1912                 } else if (get_irn_op(a) == op_Minus) {
1913                         n = new_rd_Sub(
1914                                         get_irn_dbg_info(n),
1915                                         current_ir_graph,
1916                                         get_irn_n(n, -1),
1917                                         b,
1918                                         get_Minus_op(a),
1919                                         mode);
1920                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1921                 } else if (get_irn_op(b) == op_Minus) {
1922                         n = new_rd_Sub(
1923                                         get_irn_dbg_info(n),
1924                                         current_ir_graph,
1925                                         get_irn_n(n, -1),
1926                                         a,
1927                                         get_Minus_op(b),
1928                                         mode);
1929                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1930                 }
1931                 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1932                 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1933                         ir_node *ma = get_Mul_left(a);
1934                         ir_node *mb = get_Mul_right(a);
1935
1936                         if (b == ma) {
1937                                 ir_node *blk = get_irn_n(n, -1);
1938                                 n = new_rd_Mul(
1939                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1940                                                 ma,
1941                                                 new_rd_Add(
1942                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1943                                                         mb,
1944                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1945                                                         mode),
1946                                                 mode);
1947                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1948                         } else if (b == mb) {
1949                                 ir_node *blk = get_irn_n(n, -1);
1950                                 n = new_rd_Mul(
1951                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1952                                                 mb,
1953                                                 new_rd_Add(
1954                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1955                                                         ma,
1956                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1957                                                         mode),
1958                                                 mode);
1959                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1960                         }
1961                 }
1962                 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1963                 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1964                         ir_node *ma = get_Mul_left(b);
1965                         ir_node *mb = get_Mul_right(b);
1966
1967                         if (a == ma) {
1968                                 ir_node *blk = get_irn_n(n, -1);
1969                                 n = new_rd_Mul(
1970                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1971                                                 ma,
1972                                                 new_rd_Add(
1973                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1974                                                         mb,
1975                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1976                                                         mode),
1977                                                 mode);
1978                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1979                         } else if (a == mb) {
1980                                 ir_node *blk = get_irn_n(n, -1);
1981                                 n = new_rd_Mul(
1982                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1983                                                 mb,
1984                                                 new_rd_Add(
1985                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1986                                                         ma,
1987                                                         new_r_Const_long(current_ir_graph, blk, mode, 1),
1988                                                         mode),
1989                                                 mode);
1990                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1991                         }
1992                 }
1993         }
1994         return n;
1995 }  /* transform_node_Add */
1996
1997 /**
1998  * Do the AddSub optimization, then Transform
1999  *   Constant folding on Phi
2000  *   Sub(0,a)          -> Minus(a)
2001  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2002  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2003  */
2004 static ir_node *transform_node_Sub(ir_node *n) {
2005         ir_mode *mode;
2006         ir_node *oldn = n;
2007         ir_node *a, *b, *c;
2008
2009         n = transform_node_AddSub(n);
2010
2011         a = get_Sub_left(n);
2012         b = get_Sub_right(n);
2013
2014         HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2015
2016         mode = get_irn_mode(n);
2017
2018         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2019         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2020                 return n;
2021
2022         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2023         if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2024                 n = new_rd_Minus(
2025                                 get_irn_dbg_info(n),
2026                                 current_ir_graph,
2027                                 get_irn_n(n, -1),
2028                                 b,
2029                                 mode);
2030                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2031         }
2032         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2033         else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2034                 ir_node *ma = get_Mul_left(a);
2035                 ir_node *mb = get_Mul_right(a);
2036
2037                 if (ma == b) {
2038                         ir_node *blk = get_irn_n(n, -1);
2039                         n = new_rd_Mul(
2040                                         get_irn_dbg_info(n),
2041                                         current_ir_graph, blk,
2042                                         ma,
2043                                         new_rd_Sub(
2044                                                 get_irn_dbg_info(n),
2045                                                 current_ir_graph, blk,
2046                                                 mb,
2047                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2048                                                 mode),
2049                                         mode);
2050                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2051                 } else if (mb == b) {
2052                         ir_node *blk = get_irn_n(n, -1);
2053                         n = new_rd_Mul(
2054                                         get_irn_dbg_info(n),
2055                                         current_ir_graph, blk,
2056                                         mb,
2057                                         new_rd_Sub(
2058                                                 get_irn_dbg_info(n),
2059                                                 current_ir_graph, blk,
2060                                                 ma,
2061                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2062                                                 mode),
2063                                         mode);
2064                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2065                 }
2066         } else if (get_irn_op(a) == op_Sub) {
2067                 ir_node *x   = get_Sub_left(a);
2068                 ir_node *y   = get_Sub_right(a);
2069                 ir_node *blk = get_irn_n(n, -1);
2070                 ir_mode *m_b = get_irn_mode(b);
2071                 ir_mode *m_y = get_irn_mode(y);
2072                 ir_node *add;
2073
2074                 /* Determine the right mode for the Add. */
2075                 if (m_b == m_y)
2076                         mode = m_b;
2077                 else if (mode_is_reference(m_b))
2078                         mode = m_b;
2079                 else if (mode_is_reference(m_y))
2080                         mode = m_y;
2081                 else {
2082                         /*
2083                          * Both modes are different but none is reference,
2084                          * happens for instance in SubP(SubP(P, Iu), Is).
2085                          * We have two possibilities here: Cast or ignore.
2086                          * Currently we ignore this case.
2087                          */
2088                         return n;
2089                 }
2090
2091                 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2092
2093                 set_Sub_left(n, x);
2094                 set_Sub_right(n, add);
2095                 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2096         }
2097
2098         return n;
2099 }  /* transform_node_Sub */
2100
2101 /**
2102  * Transform Mul(a,-1) into -a.
2103  * Do constant evaluation of Phi nodes.
2104  * Do architecture dependent optimizations on Mul nodes
2105  */
2106 static ir_node *transform_node_Mul(ir_node *n) {
2107         ir_node *c, *oldn = n;
2108         ir_node *a = get_Mul_left(n);
2109         ir_node *b = get_Mul_right(n);
2110         ir_mode *mode;
2111
2112         HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2113
2114         mode = get_irn_mode(n);
2115         if (mode_is_signed(mode)) {
2116                 ir_node *r = NULL;
2117
2118                 if (value_of(a) == get_mode_minus_one(mode))
2119                         r = b;
2120                 else if (value_of(b) == get_mode_minus_one(mode))
2121                         r = a;
2122                 if (r) {
2123                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2124                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2125                         return n;
2126                 }
2127         }
2128         return arch_dep_replace_mul_with_shifts(n);
2129 }  /* transform_node_Mul */
2130
2131 /**
2132  * Transform a Div Node.
2133  */
2134 static ir_node *transform_node_Div(ir_node *n) {
2135         tarval *tv = value_of(n);
2136         ir_node *value = n;
2137
2138         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2139
2140         if (tv != tarval_bad) {
2141                 value = new_Const(get_tarval_mode(tv), tv);
2142
2143                 DBG_OPT_CSTEVAL(n, value);
2144         } else /* Try architecture dependent optimization */
2145                 value = arch_dep_replace_div_by_const(n);
2146
2147         if (value != n) {
2148                 /* Turn Div into a tuple (mem, bad, value) */
2149                 ir_node *mem = get_Div_mem(n);
2150                 ir_node *blk = get_nodes_block(n);
2151
2152                 turn_into_tuple(n, pn_Div_max);
2153                 set_Tuple_pred(n, pn_Div_M,         mem);
2154                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2155                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2156                 set_Tuple_pred(n, pn_Div_res,       value);
2157         }
2158         return n;
2159 }  /* transform_node_Div */
2160
2161 /**
2162  * Transform a Mod node.
2163  */
2164 static ir_node *transform_node_Mod(ir_node *n) {
2165         tarval *tv = value_of(n);
2166         ir_node *value = n;
2167
2168         /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2169
2170         if (tv != tarval_bad) {
2171                 value = new_Const(get_tarval_mode(tv), tv);
2172
2173                 DBG_OPT_CSTEVAL(n, value);
2174         } else /* Try architecture dependent optimization */
2175                 value = arch_dep_replace_mod_by_const(n);
2176
2177         if (value != n) {
2178                 /* Turn Mod into a tuple (mem, bad, value) */
2179                 ir_node *mem = get_Mod_mem(n);
2180                 ir_node *blk = get_nodes_block(n);
2181
2182                 turn_into_tuple(n, pn_Mod_max);
2183                 set_Tuple_pred(n, pn_Mod_M,         mem);
2184                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2185                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2186                 set_Tuple_pred(n, pn_Mod_res,       value);
2187         }
2188         return n;
2189 }  /* transform_node_Mod */
2190
2191 /**
2192  * Transform a DivMod node.
2193  */
2194 static ir_node *transform_node_DivMod(ir_node *n) {
2195         int evaluated = 0;
2196
2197         ir_node *a = get_DivMod_left(n);
2198         ir_node *b = get_DivMod_right(n);
2199         ir_mode *mode = get_irn_mode(a);
2200         tarval *ta = value_of(a);
2201         tarval *tb = value_of(b);
2202
2203         if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2204                 return n;
2205
2206         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2207
2208         if (tb != tarval_bad) {
2209                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2210                         b = new_Const (mode, get_mode_null(mode));
2211                         evaluated = 1;
2212
2213                         DBG_OPT_CSTEVAL(n, b);
2214                 } else if (ta != tarval_bad) {
2215                         tarval *resa, *resb;
2216                         resa = tarval_div (ta, tb);
2217                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2218                                                              Jmp for X result!? */
2219                         resb = tarval_mod (ta, tb);
2220                         if (resb == tarval_bad) return n; /* Causes exception! */
2221                         a = new_Const (mode, resa);
2222                         b = new_Const (mode, resb);
2223                         evaluated = 1;
2224
2225                         DBG_OPT_CSTEVAL(n, a);
2226                         DBG_OPT_CSTEVAL(n, b);
2227                 } else { /* Try architecture dependent optimization */
2228                         arch_dep_replace_divmod_by_const(&a, &b, n);
2229                         evaluated = a != NULL;
2230                 }
2231         } else if (ta == get_mode_null(mode)) {
2232                 /* 0 / non-Const = 0 */
2233                 b = a;
2234                 evaluated = 1;
2235         }
2236
2237         if (evaluated) { /* replace by tuple */
2238                 ir_node *mem = get_DivMod_mem(n);
2239                 ir_node *blk = get_nodes_block(n);
2240                 turn_into_tuple(n, pn_DivMod_max);
2241                 set_Tuple_pred(n, pn_DivMod_M,         mem);
2242                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2243                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
2244                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
2245                 set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2246         }
2247
2248         return n;
2249 }  /* transform_node_DivMod */
2250
2251 /**
2252  * Optimize Abs(x) into  x if x is Confirmed >= 0
2253  * Optimize Abs(x) into -x if x is Confirmed <= 0
2254  */
2255 static ir_node *transform_node_Abs(ir_node *n) {
2256         ir_node        *oldn = n;
2257         ir_node        *a = get_Abs_op(n);
2258         value_classify_sign sign = classify_value_sign(a);
2259
2260         if (sign == value_classified_negative) {
2261                 ir_mode *mode = get_irn_mode(n);
2262
2263                 /*
2264                  * We can replace the Abs by -x here.
2265                  * We even could add a new Confirm here.
2266                  *
2267                  * Note that -x would create a new node, so we could
2268                  * not run it in the equivalent_node() context.
2269                  */
2270                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2271                                 get_irn_n(n, -1), a, mode);
2272
2273                 DBG_OPT_CONFIRM(oldn, n);
2274         } else if (sign == value_classified_positive) {
2275                 /* n is positive, Abs is not needed */
2276                 n = a;
2277
2278                 DBG_OPT_CONFIRM(oldn, n);
2279         }
2280
2281         return n;
2282 }  /* transform_node_Abs */
2283
2284 /**
2285  * Transform a Cond node.
2286  *
2287  * Replace the Cond by a Jmp if it branches on a constant
2288  * condition.
2289  */
2290 static ir_node *transform_node_Cond(ir_node *n) {
2291
2292         ir_node *jmp;
2293         ir_node *a = get_Cond_selector(n);
2294         tarval *ta = value_of(a);
2295
2296         /* we need block info which is not available in floating irgs */
2297         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2298                 return n;
2299
2300         if ((ta != tarval_bad) &&
2301             (get_irn_mode(a) == mode_b) &&
2302             (get_opt_unreachable_code())) {
2303                 /* It's a boolean Cond, branching on a boolean constant.
2304                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2305                 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2306                 turn_into_tuple(n, pn_Cond_max);
2307                 if (ta == tarval_b_true) {
2308                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2309                         set_Tuple_pred(n, pn_Cond_true, jmp);
2310                 } else {
2311                         set_Tuple_pred(n, pn_Cond_false, jmp);
2312                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2313                 }
2314                 /* We might generate an endless loop, so keep it alive. */
2315                 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2316         }
2317         return n;
2318 }  /* transform_node_Cond */
2319
2320 /**
2321  * Transform an And.
2322  */
2323 static ir_node *transform_node_And(ir_node *n) {
2324         ir_node *c, *oldn = n;
2325         ir_node *a = get_And_left(n);
2326         ir_node *b = get_And_right(n);
2327
2328         HANDLE_BINOP_PHI(tarval_and, a,b,c);
2329         return n;
2330 }  /* transform_node_And */
2331
2332 /**
2333  * Transform an Eor.
2334  */
2335 static ir_node *transform_node_Eor(ir_node *n) {
2336         ir_node *c, *oldn = n;
2337         ir_node *a = get_Eor_left(n);
2338         ir_node *b = get_Eor_right(n);
2339         ir_mode *mode = get_irn_mode(n);
2340
2341         HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2342
2343         if (a == b) {
2344                 /* a ^ a = 0 */
2345                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2346                                  mode, get_mode_null(mode));
2347                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2348         } else if ((mode == mode_b)
2349                    && (get_irn_op(a) == op_Proj)
2350                    && (get_irn_mode(a) == mode_b)
2351                    && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2352                    && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2353                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2354                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2355                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2356
2357                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2358         } else if ((mode == mode_b)
2359                && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2360                 /* The Eor is a Not. Replace it by a Not. */
2361                 /*   ????!!!Extend to bitfield 1111111. */
2362                 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2363
2364                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2365         }
2366
2367         return n;
2368 }  /* transform_node_Eor */
2369
2370 /**
2371  * Transform a Not.
2372  */
2373 static ir_node *transform_node_Not(ir_node *n) {
2374         ir_node *c, *oldn = n;
2375         ir_node *a = get_Not_op(n);
2376
2377         HANDLE_UNOP_PHI(tarval_not,a,c);
2378
2379         /* check for a boolean Not */
2380         if (   (get_irn_mode(n) == mode_b)
2381             && (get_irn_op(a) == op_Proj)
2382             && (get_irn_mode(a) == mode_b)
2383             && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2384                 /* We negate a Cmp. The Cmp has the negated result anyways! */
2385                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2386                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2387                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2388         }
2389         return n;
2390 }  /* transform_node_Not */
2391
2392 /**
2393  * Transform a Minus.
2394  */
2395 static ir_node *transform_node_Minus(ir_node *n) {
2396         ir_node *c, *oldn = n;
2397         ir_node *a = get_Minus_op(n);
2398
2399         HANDLE_UNOP_PHI(tarval_neg,a,c);
2400         return n;
2401 }  /* transform_node_Minus */
2402
2403 /**
2404  * Transform a Cast_type(Const) into a new Const_type
2405  */
2406 static ir_node *transform_node_Cast(ir_node *n) {
2407         ir_node *oldn = n;
2408         ir_node *pred = get_Cast_op(n);
2409         ir_type *tp = get_irn_type(n);
2410
2411         if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2412                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2413                         get_Const_tarval(pred), tp);
2414                 DBG_OPT_CSTEVAL(oldn, n);
2415         } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2416                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2417                         get_SymConst_kind(pred), tp);
2418                 DBG_OPT_CSTEVAL(oldn, n);
2419         }
2420
2421         return n;
2422 }  /* transform_node_Cast */
2423
2424 /**
2425  * Transform a Proj(Div) with a non-zero value.
2426  * Removes the exceptions and routes the memory to the NoMem node.
2427  */
2428 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2429         ir_node *n = get_Proj_pred(proj);
2430         ir_node *b = get_Div_right(n);
2431         ir_node *confirm;
2432         long proj_nr;
2433
2434         if (value_not_zero(b, &confirm)) {
2435                 /* div(x, y) && y != 0 */
2436                 proj_nr = get_Proj_proj(proj);
2437                 if (proj_nr == pn_Div_X_except) {
2438                         /* we found an exception handler, remove it */
2439                         DBG_OPT_EXC_REM(proj);
2440                         return new_Bad();
2441                 } else if (proj_nr == pn_Div_M) {
2442                         ir_node *res = get_Div_mem(n);
2443                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2444
2445                         if (confirm) {
2446                                 /* This node can only float up to the Confirm block */
2447                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2448                         }
2449                         set_irn_pinned(n, op_pin_state_floats);
2450                         /* this is a Div without exception, we can remove the memory edge */
2451                         set_Div_mem(n, new_mem);
2452                         return res;
2453                 }
2454         }
2455         return proj;
2456 }  /* transform_node_Proj_Div */
2457
2458 /**
2459  * Transform a Proj(Mod) with a non-zero value.
2460  * Removes the exceptions and routes the memory to the NoMem node.
2461  */
2462 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2463         ir_node *n = get_Proj_pred(proj);
2464         ir_node *b = get_Mod_right(n);
2465         ir_node *confirm;
2466         long proj_nr;
2467
2468         if (value_not_zero(b, &confirm)) {
2469                 /* mod(x, y) && y != 0 */
2470                 proj_nr = get_Proj_proj(proj);
2471
2472                 if (proj_nr == pn_Mod_X_except) {
2473                         /* we found an exception handler, remove it */
2474                         DBG_OPT_EXC_REM(proj);
2475                         return new_Bad();
2476                 } else if (proj_nr == pn_Mod_M) {
2477                         ir_node *res = get_Mod_mem(n);
2478                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2479
2480                         if (confirm) {
2481                                 /* This node can only float up to the Confirm block */
2482                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2483                         }
2484                         set_irn_pinned(n, op_pin_state_floats);
2485                         /* this is a Mod without exception, we can remove the memory edge */
2486                         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2487                         return res;
2488                 } else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2489                         /* a % a = 0 if a != 0 */
2490                         ir_mode *mode = get_irn_mode(proj);
2491                         ir_node *res  = new_Const(mode, get_mode_null(mode));
2492
2493                         DBG_OPT_CSTEVAL(n, res);
2494                         return res;
2495                 }
2496         }
2497         return proj;
2498 }  /* transform_node_Proj_Mod */
2499
2500 /**
2501  * Transform a Proj(DivMod) with a non-zero value.
2502  * Removes the exceptions and routes the memory to the NoMem node.
2503  */
2504 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2505         ir_node *n = get_Proj_pred(proj);
2506         ir_node *b = get_DivMod_right(n);
2507         ir_node *confirm;
2508         long proj_nr;
2509
2510         if (value_not_zero(b, &confirm)) {
2511                 /* DivMod(x, y) && y != 0 */
2512                 proj_nr = get_Proj_proj(proj);
2513
2514                 if (proj_nr == pn_DivMod_X_except) {
2515                         /* we found an exception handler, remove it */
2516                         DBG_OPT_EXC_REM(proj);
2517                         return new_Bad();
2518                 } else if (proj_nr == pn_DivMod_M) {
2519                         ir_node *res = get_DivMod_mem(n);
2520                         ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2521
2522                         if (confirm) {
2523                                 /* This node can only float up to the Confirm block */
2524                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2525                         }
2526                         set_irn_pinned(n, op_pin_state_floats);
2527                         /* this is a DivMod without exception, we can remove the memory edge */
2528                         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2529                         return res;
2530                 } else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2531                         /* a % a = 0 if a != 0 */
2532                         ir_mode *mode = get_irn_mode(proj);
2533                         ir_node *res  = new_Const(mode, get_mode_null(mode));
2534
2535                         DBG_OPT_CSTEVAL(n, res);
2536                         return res;
2537                 }
2538         }
2539         return proj;
2540 }  /* transform_node_Proj_DivMod */
2541
2542 /**
2543  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2544  */
2545 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2546         if (get_opt_unreachable_code()) {
2547                 ir_node *n = get_Proj_pred(proj);
2548                 ir_node *b = get_Cond_selector(n);
2549
2550                 if (mode_is_int(get_irn_mode(b))) {
2551                         tarval *tb = value_of(b);
2552
2553                         if (tb != tarval_bad) {
2554                                 /* we have a constant switch */
2555                                 long num = get_Proj_proj(proj);
2556
2557                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2558                                         if (get_tarval_long(tb) == num) {
2559                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
2560                                                  * in a block. This case is optimized away in optimize_cf(). */
2561                                                 return proj;
2562                                         } else {
2563                                                 /* this case will NEVER be taken, kill it */
2564                                                 return new_Bad();
2565                                         }
2566                                 }
2567                         }
2568                 }
2569         }
2570         return proj;
2571 }  /* transform_node_Proj_Cond */
2572
2573 /**
2574  * Normalizes and optimizes Cmp nodes.
2575  */
2576 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2577         if (get_opt_reassociation()) {
2578                 ir_node *n     = get_Proj_pred(proj);
2579                 ir_node *left  = get_Cmp_left(n);
2580                 ir_node *right = get_Cmp_right(n);
2581                 ir_node *c     = NULL;
2582                 tarval *tv     = NULL;
2583                 int changed    = 0;
2584                 ir_mode *mode  = NULL;
2585                 long proj_nr   = get_Proj_proj(proj);
2586
2587                 /*
2588                  * First step: normalize the compare op
2589                  * by placing the constant on the right site
2590                  * or moving the lower address node to the left.
2591                  * We ignore the case that both are constants
2592                  * this case should be optimized away.
2593                  */
2594                 if (get_irn_op(right) == op_Const) {
2595                         c = right;
2596                 } else if (get_irn_op(left) == op_Const) {
2597                         c     = left;
2598                         left  = right;
2599                         right = c;
2600
2601                         proj_nr = get_inversed_pnc(proj_nr);
2602                         changed |= 1;
2603                 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2604                         ir_node *t = left;
2605
2606                         left  = right;
2607                         right = t;
2608
2609                         proj_nr = get_inversed_pnc(proj_nr);
2610                         changed |= 1;
2611                 }
2612
2613                 /*
2614                  * Second step: Try to reduce the magnitude
2615                  * of a constant. This may help to generate better code
2616                  * later and may help to normalize more compares.
2617                  * Of course this is only possible for integer values.
2618                  */
2619                 if (c) {
2620                         mode = get_irn_mode(c);
2621                         tv = get_Const_tarval(c);
2622
2623                         if (tv != tarval_bad) {
2624                                 /* the following optimization is possible on modes without Overflow
2625                                  * on Unary Minus or on == and !=:
2626                                  * -a CMP c  ==>  a swap(CMP) -c
2627                                  *
2628                                  * Beware: for two-complement Overflow may occur, so only == and != can
2629                                  * be optimized, see this:
2630                                  * -MININT < 0 =/=> MININT > 0 !!!
2631                                  */
2632                                 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2633                                     (!mode_overflow_on_unary_Minus(mode) ||
2634                                     (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2635                                         left = get_Minus_op(left);
2636                                         tv = tarval_sub(get_mode_null(mode), tv);
2637
2638                                         proj_nr = get_inversed_pnc(proj_nr);
2639                                         changed |= 2;
2640                                 }
2641
2642                                 /* for integer modes, we have more */
2643                                 if (mode_is_int(mode)) {
2644                                         /* Ne includes Unordered which is not possible on integers.
2645                                          * However, frontends often use this wrong, so fix it here */
2646                                         if (proj_nr & pn_Cmp_Uo) {
2647                                                 proj_nr &= ~pn_Cmp_Uo;
2648                                                 set_Proj_proj(proj, proj_nr);
2649                                         }
2650
2651                                         /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2652                                         if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2653                                                 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2654                                                 tv = tarval_sub(tv, get_mode_one(mode));
2655
2656                                                 proj_nr ^= pn_Cmp_Eq;
2657                                                 changed |= 2;
2658                                         }
2659                                         /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2660                                         else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2661                                                 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2662                                                 tv = tarval_add(tv, get_mode_one(mode));
2663
2664                                                 proj_nr ^= pn_Cmp_Eq;
2665                                                 changed |= 2;
2666                                         }
2667
2668                                         /* the following reassociations work only for == and != */
2669                                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2670
2671                                                 /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2672                                                 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2673                                                         right = get_Sub_right(left);
2674                                                         left  = get_Sub_left(left);
2675
2676                                                         tv = value_of(right);
2677                                                         changed = 1;
2678                                                 }
2679
2680                                                 if (tv != tarval_bad) {
2681                                                         ir_op *op = get_irn_op(left);
2682
2683                                                         /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2684                                                         if (op == op_Sub) {
2685                                                                 ir_node *c1 = get_Sub_right(left);
2686                                                                 tarval *tv2 = value_of(c1);
2687
2688                                                                 if (tv2 != tarval_bad) {
2689                                                                         tv2 = tarval_add(tv, value_of(c1));
2690
2691                                                                         if (tv2 != tarval_bad) {
2692                                                                                 left    = get_Sub_left(left);
2693                                                                                 tv      = tv2;
2694                                                                                 changed |= 2;
2695                                                                         }
2696                                                                 }
2697                                                         }
2698                                                         /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2699                                                         else if (op == op_Add) {
2700                                                                 ir_node *a_l = get_Add_left(left);
2701                                                                 ir_node *a_r = get_Add_right(left);
2702                                                                 ir_node *a;
2703                                                                 tarval *tv2;
2704
2705                                                                 if (get_irn_op(a_l) == op_Const) {
2706                                                                         a = a_r;
2707                                                                         tv2 = value_of(a_l);
2708                                                                 } else {
2709                                                                         a = a_l;
2710                                                                         tv2 = value_of(a_r);
2711                                                                 }
2712
2713                                                                 if (tv2 != tarval_bad) {
2714                                                                         tv2 = tarval_sub(tv, tv2);
2715
2716                                                                         if (tv2 != tarval_bad) {
2717                                                                                 left    = a;
2718                                                                                 tv      = tv2;
2719                                                                                 changed |= 2;
2720                                                                         }
2721                                                                 }
2722                                                         }
2723                                                         /* -a == c ==> a == -c, -a != c ==> a != -c */
2724                                                         else if (op == op_Minus) {
2725                                                                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2726
2727                                                                 if (tv2 != tarval_bad) {
2728                                                                         left    = get_Minus_op(left);
2729                                                                         tv      = tv2;
2730                                                                         changed |= 2;
2731                                                                 }
2732                                                         }
2733                                                 }
2734                                         } /* == or != */
2735                                         /* the following reassociations work only for <= */
2736                                         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2737                                                 if (tv != tarval_bad) {
2738                                                         ir_op *op = get_irn_op(left);
2739
2740                                                         /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2741                                                         if (op == op_Abs) {
2742                                                         }
2743                                                 }
2744                                         }
2745                                 } /* mode_is_int */
2746
2747                                 /*
2748                                  * optimization for AND:
2749                                  * Optimize:
2750                                  *   And(x, C) == C  ==>  And(x, C) != 0
2751                                  *   And(x, C) != C  ==>  And(X, C) == 0
2752                                  *
2753                                  * if C is a single Bit constant.
2754                                  */
2755                                 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2756                                     (get_irn_op(left) == op_And)) {
2757                                         if (is_single_bit_tarval(tv)) {
2758                                                 /* check for Constant's match. We have check hare the tarvals,
2759                                                    because our const might be changed */
2760                                                 ir_node *la = get_And_left(left);
2761                                                 ir_node *ra = get_And_right(left);
2762                                                 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2763                                                     (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2764                                                                 /* fine: do the transformation */
2765                                                                 tv = get_mode_null(get_tarval_mode(tv));
2766                                                                 proj_nr ^= pn_Cmp_Leg;
2767                                                                 changed |= 2;
2768                                                 }
2769                                         }
2770                                 }
2771                         } /* tarval != bad */
2772                 }
2773
2774                 if (changed) {
2775                         ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2776
2777                         if (changed & 2)      /* need a new Const */
2778                                 right = new_Const(mode, tv);
2779
2780                         /* create a new compare */
2781                         n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2782                                 left, right);
2783
2784                         set_Proj_pred(proj, n);
2785                         set_Proj_proj(proj, proj_nr);
2786                 }
2787         }
2788         return proj;
2789 }  /* transform_node_Proj_Cmp */
2790
2791 /**
2792  * Does all optimizations on nodes that must be done on it's Proj's
2793  * because of creating new nodes.
2794  */
2795 static ir_node *transform_node_Proj(ir_node *proj) {
2796         ir_node *n = get_Proj_pred(proj);
2797
2798         switch (get_irn_opcode(n)) {
2799         case iro_Div:
2800                 return transform_node_Proj_Div(proj);
2801
2802         case iro_Mod:
2803                 return transform_node_Proj_Mod(proj);
2804
2805         case iro_DivMod:
2806                 return transform_node_Proj_DivMod(proj);
2807
2808         case iro_Cond:
2809                 return transform_node_Proj_Cond(proj);
2810
2811         case iro_Cmp:
2812                 return transform_node_Proj_Cmp(proj);
2813
2814         case iro_Tuple:
2815                 /* should not happen, but if it does will be optimized away */
2816                 return equivalent_node_Proj(proj);
2817
2818         default:
2819                 /* do nothing */
2820                 return proj;
2821         }
2822 }  /* transform_node_Proj */
2823
2824 /**
2825  * Move Confirms down through Phi nodes.
2826  */
2827 static ir_node *transform_node_Phi(ir_node *phi) {
2828         int i, n;
2829         ir_mode *mode = get_irn_mode(phi);
2830
2831         if (mode_is_reference(mode)) {
2832                 n = get_irn_arity(phi);
2833
2834                 /* Beware of Phi0 */
2835                 if (n > 0) {
2836                         ir_node *pred = get_irn_n(phi, 0);
2837                         ir_node *bound, *new_Phi, *block, **in;
2838                         pn_Cmp  pnc;
2839
2840                         if (! is_Confirm(pred))
2841                                 return phi;
2842
2843                         bound = get_Confirm_bound(pred);
2844                         pnc   = get_Confirm_cmp(pred);
2845
2846                         NEW_ARR_A(ir_node *, in, n);
2847                         in[0] = get_Confirm_value(pred);
2848
2849                         for (i = 1; i < n; ++i) {
2850                                 pred = get_irn_n(phi, i);
2851
2852                                 if (! is_Confirm(pred) ||
2853                                         get_Confirm_bound(pred) != bound ||
2854                                         get_Confirm_cmp(pred) != pnc)
2855                                         return phi;
2856                                 in[i] = get_Confirm_value(pred);
2857                         }
2858                         /* move the Confirm nodes "behind" the Phi */
2859                         block = get_irn_n(phi, -1);
2860                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2861                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2862                 }
2863         }
2864         return phi;
2865 }  /* transform_node_Phi */
2866
2867 /**
2868  * Returns the operands of a commutative bin-op, if one operand is
2869  * a const, it is returned as the second one.
2870  */
2871 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
2872         ir_node *op_a = get_binop_left(binop);
2873         ir_node *op_b = get_binop_right(binop);
2874
2875         assert(is_op_commutative(get_irn_op(binop)));
2876
2877         if (get_irn_op(op_a) == op_Const) {
2878                 *a = op_b;
2879                 *c = op_a;
2880         } else {
2881                 *a = op_a;
2882                 *c = op_b;
2883         }
2884 }  /* get_comm_Binop_Ops */
2885
2886 /**
2887  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2888  * Such pattern may arise in bitfield stores.
2889  *
2890  * value  c4                  value      c4 & c2
2891  *    AND     c3                    AND           c1 | c3
2892  *        OR     c2      ===>               OR
2893  *           AND    c1
2894  *               OR
2895  */
2896 static ir_node *transform_node_Or_bf_store(ir_node *or) {
2897         ir_node *and, *c1;
2898         ir_node *or_l, *c2;
2899         ir_node *and_l, *c3;
2900         ir_node *value, *c4;
2901         ir_node *new_and, *new_const, *block;
2902         ir_mode *mode = get_irn_mode(or);
2903
2904         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2905
2906         get_comm_Binop_Ops(or, &and, &c1);
2907         if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2908                 return or;
2909
2910         get_comm_Binop_Ops(and, &or_l, &c2);
2911         if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2912                 return or;
2913
2914         get_comm_Binop_Ops(or_l, &and_l, &c3);
2915         if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2916                 return or;
2917
2918         get_comm_Binop_Ops(and_l, &value, &c4);
2919         if (get_irn_op(c4) != op_Const)
2920                 return or;
2921
2922         /* ok, found the pattern, check for conditions */
2923         assert(mode == get_irn_mode(and));
2924         assert(mode == get_irn_mode(or_l));
2925         assert(mode == get_irn_mode(and_l));
2926
2927         tv1 = get_Const_tarval(c1);
2928         tv2 = get_Const_tarval(c2);
2929         tv3 = get_Const_tarval(c3);
2930         tv4 = get_Const_tarval(c4);
2931
2932         tv = tarval_or(tv4, tv2);
2933         if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2934                 /* have at least one 0 at the same bit position */
2935                 return or;
2936         }
2937
2938         n_tv4 = tarval_not(tv4);
2939         if (tv3 != tarval_and(tv3, n_tv4)) {
2940                 /* bit in the or_mask is outside the and_mask */
2941                 return or;
2942         }
2943
2944         n_tv2 = tarval_not(tv2);
2945         if (tv1 != tarval_and(tv1, n_tv2)) {
2946                 /* bit in the or_mask is outside the and_mask */
2947                 return or;
2948         }
2949
2950         /* ok, all conditions met */
2951         block = get_irn_n(or, -1);
2952
2953         new_and = new_r_And(current_ir_graph, block,
2954                 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2955
2956         new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2957
2958         set_Or_left(or, new_and);
2959         set_Or_right(or, new_const);
2960
2961         /* check for more */
2962         return transform_node_Or_bf_store(or);
2963 }  /* transform_node_Or_bf_store */
2964
2965 /**
2966  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2967  */
2968 static ir_node *transform_node_Or_Rot(ir_node *or) {
2969         ir_mode *mode = get_irn_mode(or);
2970         ir_node *shl, *shr, *block;
2971         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2972         tarval *tv1, *tv2;
2973
2974         if (! mode_is_int(mode))
2975                 return or;
2976
2977         shl = get_binop_left(or);
2978         shr = get_binop_right(or);
2979
2980         if (get_irn_op(shl) == op_Shr) {
2981                 if (get_irn_op(shr) != op_Shl)
2982                         return or;
2983
2984                 irn = shl;
2985                 shl = shr;
2986                 shr = irn;
2987         } else if (get_irn_op(shl) != op_Shl) {
2988                 return or;
2989         } else if (get_irn_op(shr) != op_Shr) {
2990                 return or;
2991         }
2992         x = get_Shl_left(shl);
2993         if (x != get_Shr_left(shr))
2994                 return or;
2995
2996         c1 = get_Shl_right(shl);
2997         c2 = get_Shr_right(shr);
2998         if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2999                 tv1 = get_Const_tarval(c1);
3000                 if (! tarval_is_long(tv1))
3001                         return or;
3002
3003                 tv2 = get_Const_tarval(c2);
3004                 if (! tarval_is_long(tv2))
3005                         return or;
3006
3007                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3008                         != get_mode_size_bits(mode))
3009                         return or;
3010
3011                 /* yet, condition met */
3012                 block = get_irn_n(or, -1);
3013
3014                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3015
3016                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3017                 return n;
3018         } else if (get_irn_op(c1) == op_Sub) {
3019                 v   = c2;
3020                 sub = c1;
3021
3022                 if (get_Sub_right(sub) != v)
3023                         return or;
3024
3025                 c1 = get_Sub_left(sub);
3026                 if (get_irn_op(c1) != op_Const)
3027                         return or;
3028
3029                 tv1 = get_Const_tarval(c1);
3030                 if (! tarval_is_long(tv1))
3031                         return or;
3032
3033                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3034                         return or;
3035
3036                 /* yet, condition met */
3037                 block = get_nodes_block(or);
3038
3039                 /* a Rot right is not supported, so use a rot left */
3040                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
3041
3042                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3043                 return n;
3044         } else if (get_irn_op(c2) == op_Sub) {
3045                 v   = c1;
3046                 sub = c2;
3047
3048                 c1 = get_Sub_left(sub);
3049                 if (get_irn_op(c1) != op_Const)
3050                         return or;
3051
3052                 tv1 = get_Const_tarval(c1);
3053                 if (! tarval_is_long(tv1))
3054                         return or;
3055
3056                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3057                         return or;
3058
3059                 /* yet, condition met */
3060                 block = get_irn_n(or, -1);
3061
3062                 /* a Rot Left */
3063                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3064
3065                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3066                 return n;
3067         }
3068
3069         return or;
3070 }  /* transform_node_Or_Rot */
3071
3072 /**
3073  * Transform an Or.
3074  */
3075 static ir_node *transform_node_Or(ir_node *n) {
3076         ir_node *c, *oldn = n;
3077         ir_node *a = get_Or_left(n);
3078         ir_node *b = get_Or_right(n);
3079
3080         HANDLE_BINOP_PHI(tarval_or, a,b,c);
3081
3082         n = transform_node_Or_bf_store(n);
3083         n = transform_node_Or_Rot(n);
3084
3085         return n;
3086 }  /* transform_node_Or */
3087
3088
3089 /* forward */
3090 static ir_node *transform_node(ir_node *n);
3091
3092 /**
3093  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3094  *
3095  * Should be moved to reassociation?
3096  */
3097 static ir_node *transform_node_shift(ir_node *n) {
3098         ir_node *left, *right;
3099         tarval *tv1, *tv2, *res;
3100         ir_mode *mode;
3101         int modulo_shf, flag;
3102
3103         left = get_binop_left(n);
3104
3105         /* different operations */
3106         if (get_irn_op(left) != get_irn_op(n))
3107                 return n;
3108
3109         right = get_binop_right(n);
3110         tv1 = value_of(right);
3111         if (tv1 == tarval_bad)
3112                 return n;
3113
3114         tv2 = value_of(get_binop_right(left));
3115         if (tv2 == tarval_bad)
3116                 return n;
3117
3118         res = tarval_add(tv1, tv2);
3119
3120         /* beware: a simple replacement works only, if res < modulo shift */
3121         mode = get_irn_mode(n);
3122
3123         flag = 0;
3124
3125         modulo_shf = get_mode_modulo_shift(mode);
3126         if (modulo_shf > 0) {
3127                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3128
3129                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3130                         flag = 1;
3131         } else
3132                 flag = 1;
3133
3134         if (flag) {
3135                 /* ok, we can replace it */
3136                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3137
3138                 in[0] = get_binop_left(left);
3139                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3140
3141                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3142
3143                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3144
3145                 return transform_node(irn);
3146         }
3147         return n;
3148 }  /* transform_node_shift */
3149
3150 /**
3151  * Transform a Shr.
3152  */
3153 static ir_node *transform_node_Shr(ir_node *n) {
3154         ir_node *c, *oldn = n;
3155         ir_node *a = get_Shr_left(n);
3156         ir_node *b = get_Shr_right(n);
3157
3158         HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3159         return transform_node_shift(n);
3160 }  /* transform_node_Shr */
3161
3162 /**
3163  * Transform a Shrs.
3164  */
3165 static ir_node *transform_node_Shrs(ir_node *n) {
3166         ir_node *c, *oldn = n;
3167         ir_node *a = get_Shrs_left(n);
3168         ir_node *b = get_Shrs_right(n);
3169
3170         HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3171         return transform_node_shift(n);
3172 }  /* transform_node_Shrs */
3173
3174 /**
3175  * Transform a Shl.
3176  */
3177 static ir_node *transform_node_Shl(ir_node *n) {
3178         ir_node *c, *oldn = n;
3179         ir_node *a = get_Shl_left(n);
3180         ir_node *b = get_Shl_right(n);
3181
3182         HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3183         return transform_node_shift(n);
3184 }  /* transform_node_Shl */
3185
3186 /**
3187  * Remove dead blocks and nodes in dead blocks
3188  * in keep alive list.  We do not generate a new End node.
3189  */
3190 static ir_node *transform_node_End(ir_node *n) {
3191         int i, j, n_keepalives = get_End_n_keepalives(n);
3192         ir_node **in;
3193
3194         NEW_ARR_A(ir_node *, in, n_keepalives);
3195
3196         for (i = j = 0; i < n_keepalives; ++i) {
3197                 ir_node *ka = get_End_keepalive(n, i);
3198                 if (is_Block(ka)) {
3199                         if (! is_Block_dead(ka)) {
3200                                 in[j++] = ka;
3201                         }
3202                         continue;
3203                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3204                         continue;
3205                 }
3206                 /* FIXME: beabi need to keep a Proj(M) */
3207                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3208                         in[j++] = ka;
3209         }
3210         if (j != n_keepalives)
3211                 set_End_keepalives(n, j, in);
3212         return n;
3213 }  /* transform_node_End */
3214
3215 /**
3216  * Optimize a Mux into some simpler cases.
3217  */
3218 static ir_node *transform_node_Mux(ir_node *n) {
3219         ir_node *oldn = n, *sel = get_Mux_sel(n);
3220         ir_mode *mode = get_irn_mode(n);
3221
3222         if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3223                 ir_node *cmp = get_Proj_pred(sel);
3224                 long proj_nr = get_Proj_proj(sel);
3225                 ir_node *f   =  get_Mux_false(n);
3226                 ir_node *t   = get_Mux_true(n);
3227
3228                 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3229                         ir_node *block = get_irn_n(n, -1);
3230
3231                         /*
3232                          * Note: normalization puts the constant on the right site,
3233                          * so we check only one case.
3234                          *
3235                          * Note further that these optimization work even for floating point
3236                          * with NaN's because -NaN == NaN.
3237                          * However, if +0 and -0 is handled differently, we cannot use the first one.
3238                          */
3239                         if (get_irn_op(f) == op_Minus &&
3240                                 get_Minus_op(f)   == t &&
3241                                 get_Cmp_left(cmp) == t) {
3242
3243                                 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
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                                                 t, mode);
3249                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3250                                         return n;
3251                                 } else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
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                                                         t, 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                         } else if (get_irn_op(t) == op_Minus &&
3266                                    get_Minus_op(t)   == f &&
3267                                    get_Cmp_left(cmp) == f) {
3268
3269                                 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3270                                         /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
3271                                         n = new_rd_Abs(get_irn_dbg_info(n),
3272                                                         current_ir_graph,
3273                                                         block,
3274                                                         f, mode);
3275                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3276                                         return n;
3277                                 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3278                                         /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
3279                                         n = new_rd_Abs(get_irn_dbg_info(n),
3280                                                         current_ir_graph,
3281                                                         block,
3282                                                         f, mode);
3283                                         n = new_rd_Minus(get_irn_dbg_info(n),
3284                                                         current_ir_graph,
3285                                                         block,
3286                                                         n, mode);
3287
3288                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3289                                         return n;
3290                                 }
3291                         }
3292
3293                         if (mode_is_int(mode) && mode_is_signed(mode) &&
3294                                 get_mode_arithmetic(mode) == irma_twos_complement) {
3295                                 ir_node *x = get_Cmp_left(cmp);
3296
3297                                 /* the following optimization works only with signed integer two-complement mode */
3298
3299                                 if (mode == get_irn_mode(x)) {
3300                                         /*
3301                                          * FIXME: this restriction is two rigid, as it would still
3302                                          * work if mode(x) = Hs and mode == Is, but at least it removes
3303                                          * all wrong cases.
3304                                          */
3305                                         if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3306                                             classify_Const(t) == CNST_ALL_ONE &&
3307                                             classify_Const(f) == CNST_NULL) {
3308                                                 /*
3309                                                  * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3310                                                  * Conditions:
3311                                                  * T must be signed.
3312                                                  */
3313                                                 n = new_rd_Shrs(get_irn_dbg_info(n),
3314                                                                 current_ir_graph, block, x,
3315                                                                 new_r_Const_long(current_ir_graph, block, mode_Iu,
3316                                                                 get_mode_size_bits(mode) - 1),
3317                                                                 mode);
3318                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3319                                                 return n;
3320                                         } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3321                                                    classify_Const(t) == CNST_ONE &&
3322                                                    classify_Const(f) == CNST_NULL) {
3323                                                 /*
3324                                                  * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3325                                                  * Conditions:
3326                                                  * T must be signed.
3327                                                  */
3328                                                 n = new_rd_Shr(get_irn_dbg_info(n),
3329                                                         current_ir_graph, block,
3330                                                         new_r_Minus(current_ir_graph, block, x, mode),
3331                                                         new_r_Const_long(current_ir_graph, block, mode_Iu,
3332                                                         get_mode_size_bits(mode) - 1),
3333                                                         mode);
3334                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3335                                                 return n;
3336                                         }
3337                                 }
3338                         }
3339                 }
3340         }
3341         return arch_transform_node_Mux(n);
3342 }  /* transform_node_Mux */
3343
3344 /**
3345  * Optimize a Psi into some simpler cases.
3346  */
3347 static ir_node *transform_node_Psi(ir_node *n) {
3348         if (is_Mux(n))
3349                 return transform_node_Mux(n);
3350
3351         return n;
3352 }  /* transform_node_Psi */
3353
3354 /**
3355  * Tries several [inplace] [optimizing] transformations and returns an
3356  * equivalent node.  The difference to equivalent_node() is that these
3357  * transformations _do_ generate new nodes, and thus the old node must
3358  * not be freed even if the equivalent node isn't the old one.
3359  */
3360 static ir_node *transform_node(ir_node *n) {
3361         if (n->op->ops.transform_node)
3362                 n = n->op->ops.transform_node(n);
3363         return n;
3364 }  /* transform_node */
3365
3366 /**
3367  * Sets the default transform node operation for an ir_op_ops.
3368  *
3369  * @param code   the opcode for the default operation
3370  * @param ops    the operations initialized
3371  *
3372  * @return
3373  *    The operations.
3374  */
3375 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3376 {
3377 #define CASE(a)                                 \
3378         case iro_##a:                                 \
3379                 ops->transform_node  = transform_node_##a;  \
3380                 break
3381
3382         switch (code) {
3383         CASE(Add);
3384         CASE(Sub);
3385         CASE(Mul);
3386         CASE(Div);
3387         CASE(Mod);
3388         CASE(DivMod);
3389         CASE(Abs);
3390         CASE(Cond);
3391         CASE(And);
3392         CASE(Or);
3393         CASE(Eor);
3394         CASE(Minus);
3395         CASE(Not);
3396         CASE(Cast);
3397         CASE(Proj);
3398         CASE(Phi);
3399         CASE(Sel);
3400         CASE(Shr);
3401         CASE(Shrs);
3402         CASE(Shl);
3403         CASE(End);
3404         CASE(Mux);
3405         CASE(Psi);
3406         default:
3407           /* leave NULL */;
3408         }
3409
3410         return ops;
3411 #undef CASE
3412 }  /* firm_set_default_transform_node */
3413
3414
3415 /* **************** Common Subexpression Elimination **************** */
3416
3417 /** The size of the hash table used, should estimate the number of nodes
3418     in a graph. */
3419 #define N_IR_NODES 512
3420
3421 /** Compares the attributes of two Const nodes. */
3422 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3423         return (get_Const_tarval(a) != get_Const_tarval(b))
3424             || (get_Const_type(a) != get_Const_type(b));
3425 }  /* node_cmp_attr_Const */
3426
3427 /** Compares the attributes of two Proj nodes. */
3428 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3429         return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3430 }  /* node_cmp_attr_Proj */
3431
3432 /** Compares the attributes of two Filter nodes. */
3433 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3434         return get_Filter_proj(a) != get_Filter_proj(b);
3435 }  /* node_cmp_attr_Filter */
3436
3437 /** Compares the attributes of two Alloc nodes. */
3438 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3439         return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3440             || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3441 }  /* node_cmp_attr_Alloc */
3442
3443 /** Compares the attributes of two Free nodes. */
3444 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3445         return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3446             || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3447 }  /* node_cmp_attr_Free */
3448
3449 /** Compares the attributes of two SymConst nodes. */
3450 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3451         return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3452             || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3453             || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3454 }  /* node_cmp_attr_SymConst */
3455
3456 /** Compares the attributes of two Call nodes. */
3457 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3458         return (get_irn_call_attr(a) != get_irn_call_attr(b));
3459 }  /* node_cmp_attr_Call */
3460
3461 /** Compares the attributes of two Sel nodes. */
3462 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3463         return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
3464             || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
3465             || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
3466             || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3467             || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
3468 }  /* node_cmp_attr_Sel */
3469
3470 /** Compares the attributes of two Phi nodes. */
3471 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3472         /* we can only enter this function if both nodes have the same number of inputs,
3473            hence it is enough to check if one of them is a Phi0 */
3474         if (is_Phi0(a)) {
3475                 /* check the Phi0 attribute */
3476                 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
3477         }
3478         return 0;
3479 }  /* node_cmp_attr_Phi */
3480
3481 /** Compares the attributes of two Conv nodes. */
3482 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3483         return get_Conv_strict(a) != get_Conv_strict(b);
3484 }  /* node_cmp_attr_Conv */
3485
3486 /** Compares the attributes of two Cast nodes. */
3487 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3488         return get_Cast_type(a) != get_Cast_type(b);
3489 }  /* node_cmp_attr_Cast */
3490
3491 /** Compares the attributes of two Load nodes. */
3492 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3493         if (get_Load_volatility(a) == volatility_is_volatile ||
3494             get_Load_volatility(b) == volatility_is_volatile)
3495                 /* NEVER do CSE on volatile Loads */
3496                 return 1;
3497
3498         return get_Load_mode(a) != get_Load_mode(b);
3499 }  /* node_cmp_attr_Load */
3500
3501 /** Compares the attributes of two Store nodes. */
3502 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3503         /* NEVER do CSE on volatile Stores */
3504         return (get_Store_volatility(a) == volatility_is_volatile ||
3505                 get_Store_volatility(b) == volatility_is_volatile);
3506 }  /* node_cmp_attr_Store */
3507
3508 /** Compares the attributes of two Confirm nodes. */
3509 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3510         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3511 }  /* node_cmp_attr_Confirm */
3512
3513 /** Compares the attributes of two ASM nodes. */
3514 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
3515         int i, n;
3516         ir_asm_constraint *ca, *cb;
3517         ident **cla, **clb;
3518
3519         if (get_ASM_text(a) != get_ASM_text(b));
3520                 return 1;
3521
3522         /* Should we really check the constraints here? Should be better, but is strange. */
3523         n = get_ASM_n_input_constraints(a);
3524         if (n != get_ASM_n_input_constraints(b))
3525                 return 0;
3526
3527         ca = get_ASM_input_constraints(a);
3528         cb = get_ASM_input_constraints(b);
3529         for (i = 0; i < n; ++i) {
3530                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3531                         return 1;
3532         }
3533
3534         n = get_ASM_n_output_constraints(a);
3535         if (n != get_ASM_n_output_constraints(b))
3536                 return 0;
3537
3538         ca = get_ASM_output_constraints(a);
3539         cb = get_ASM_output_constraints(b);
3540         for (i = 0; i < n; ++i) {
3541                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3542                         return 1;
3543         }
3544
3545         n = get_ASM_n_clobbers(a);
3546         if (n != get_ASM_n_clobbers(b))
3547                 return 0;
3548
3549         cla = get_ASM_clobbers(a);
3550         clb = get_ASM_clobbers(b);
3551         for (i = 0; i < n; ++i) {
3552                 if (cla[i] != clb[i])
3553                         return 1;
3554         }
3555         return 0;
3556 }  /* node_cmp_attr_ASM */
3557
3558 /**
3559  * Set the default node attribute compare operation for an ir_op_ops.
3560  *
3561  * @param code   the opcode for the default operation
3562  * @param ops    the operations initialized
3563  *
3564  * @return
3565  *    The operations.
3566  */
3567 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3568 {
3569 #define CASE(a)                              \
3570         case iro_##a:                              \
3571                 ops->node_cmp_attr  = node_cmp_attr_##a; \
3572                 break
3573
3574         switch (code) {
3575         CASE(Const);
3576         CASE(Proj);
3577         CASE(Filter);
3578         CASE(Alloc);
3579         CASE(Free);
3580         CASE(SymConst);
3581         CASE(Call);
3582         CASE(Sel);
3583         CASE(Phi);
3584         CASE(Conv);
3585         CASE(Cast);
3586         CASE(Load);
3587         CASE(Store);
3588         CASE(Confirm);
3589         CASE(ASM);
3590         default:
3591           /* leave NULL */;
3592         }
3593
3594         return ops;
3595 #undef CASE
3596 }  /* firm_set_default_node_cmp_attr */
3597
3598 /*
3599  * Compare function for two nodes in the hash table. Gets two
3600  * nodes as parameters.  Returns 0 if the nodes are a cse.
3601  */
3602 int identities_cmp(const void *elt, const void *key) {
3603         ir_node *a, *b;
3604         int i, irn_arity_a;
3605
3606         a = (void *)elt;
3607         b = (void *)key;
3608
3609         if (a == b) return 0;
3610
3611         if ((get_irn_op(a) != get_irn_op(b)) ||
3612             (get_irn_mode(a) != get_irn_mode(b))) return 1;
3613
3614         /* compare if a's in and b's in are of equal length */
3615         irn_arity_a = get_irn_intra_arity (a);
3616         if (irn_arity_a != get_irn_intra_arity(b))
3617                 return 1;
3618
3619         /* for block-local cse and op_pin_state_pinned nodes: */
3620         if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3621                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3622                         return 1;
3623         }
3624
3625         /* compare a->in[0..ins] with b->in[0..ins] */
3626         for (i = 0; i < irn_arity_a; i++)
3627                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3628                         return 1;
3629
3630         /*
3631          * here, we already now that the nodes are identical except their
3632          * attributes
3633          */
3634         if (a->op->ops.node_cmp_attr)
3635                 return a->op->ops.node_cmp_attr(a, b);
3636
3637         return 0;
3638 }  /* identities_cmp */
3639
3640 /*
3641  * Calculate a hash value of a node.
3642  */
3643 unsigned ir_node_hash(ir_node *node) {
3644         unsigned h;
3645         int i, irn_arity;
3646
3647         if (node->op == op_Const) {
3648                 /* special value for const, as they only differ in their tarval. */
3649                 h = HASH_PTR(node->attr.con.tv);
3650                 h = 9*h + HASH_PTR(get_irn_mode(node));
3651         } else if (node->op == op_SymConst) {
3652                 /* special value for const, as they only differ in their symbol. */
3653                 h = HASH_PTR(node->attr.symc.sym.type_p);
3654                 h = 9*h + HASH_PTR(get_irn_mode(node));
3655         } else {
3656
3657                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3658                 h = irn_arity = get_irn_intra_arity(node);
3659
3660                 /* consider all in nodes... except the block if not a control flow. */
3661                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
3662                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3663                 }
3664
3665                 /* ...mode,... */
3666                 h = 9*h + HASH_PTR(get_irn_mode(node));
3667                 /* ...and code */
3668                 h = 9*h + HASH_PTR(get_irn_op(node));
3669         }
3670
3671         return h;
3672 }  /* ir_node_hash */
3673
3674 pset *new_identities(void) {
3675         return new_pset(identities_cmp, N_IR_NODES);
3676 }  /* new_identities */
3677
3678 void del_identities(pset *value_table) {
3679         del_pset(value_table);
3680 }  /* del_identities */
3681
3682 /**
3683  * Return the canonical node computing the same value as n.
3684  *
3685  * @param value_table  The value table
3686  * @param n            The node to lookup
3687  *
3688  * Looks up the node in a hash table.
3689  *
3690  * For Const nodes this is performed in the constructor, too.  Const
3691  * nodes are extremely time critical because of their frequent use in
3692  * constant string arrays.
3693  */
3694 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3695         ir_node *o = NULL;
3696
3697         if (!value_table) return n;
3698
3699         if (get_opt_reassociation()) {
3700                 if (is_op_commutative(get_irn_op(n))) {
3701                         ir_node *l = get_binop_left(n);
3702                         ir_node *r = get_binop_right(n);
3703
3704                         /* for commutative operators perform  a OP b == b OP a */
3705                         if (get_irn_idx(l) > get_irn_idx(r)) {
3706                                 set_binop_left(n, r);
3707                                 set_binop_right(n, l);
3708                         }
3709                 }
3710         }
3711
3712         o = pset_find(value_table, n, ir_node_hash(n));
3713         if (!o) return n;
3714
3715         DBG_OPT_CSE(n, o);
3716
3717         return o;
3718 }  /* identify */
3719
3720 /**
3721  * During construction we set the op_pin_state_pinned flag in the graph right when the
3722  * optimization is performed.  The flag turning on procedure global cse could
3723  * be changed between two allocations.  This way we are safe.
3724  */
3725 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3726         ir_node *old = n;
3727
3728         n = identify(value_table, n);
3729         if (get_irn_n(old, -1) != get_irn_n(n, -1))
3730                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3731         return n;
3732 }  /* identify_cons */
3733
3734 /*
3735  * Return the canonical node computing the same value as n.
3736  * Looks up the node in a hash table, enters it in the table
3737  * if it isn't there yet.
3738  */
3739 ir_node *identify_remember(pset *value_table, ir_node *n) {
3740         ir_node *o = NULL;
3741
3742         if (!value_table) return n;
3743
3744         if (get_opt_reassociation()) {
3745                 if (is_op_commutative(get_irn_op(n))) {
3746                         ir_node *l = get_binop_left(n);
3747                         ir_node *r = get_binop_right(n);
3748                         int l_idx = get_irn_idx(l);
3749                         int r_idx = get_irn_idx(r);
3750
3751                         /* For commutative operators perform  a OP b == b OP a but keep
3752                            constants on the RIGHT side. This helps greatly in some optimizations.
3753                            Moreover we use the idx number to make the form deterministic. */
3754                         if (is_irn_constlike(l))
3755                                 l_idx = -l_idx;
3756                         if (is_irn_constlike(r))
3757                                 r_idx = -r_idx;
3758                         if (l_idx < r_idx) {
3759                                 set_binop_left(n, r);
3760                                 set_binop_right(n, l);
3761                         }
3762                 }
3763         }
3764
3765         /* lookup or insert in hash table with given hash key. */
3766         o = pset_insert(value_table, n, ir_node_hash(n));
3767
3768         if (o != n) {
3769                 DBG_OPT_CSE(n, o);
3770         }
3771
3772         return o;
3773 }  /* identify_remember */
3774
3775 /* Add a node to the identities value table. */
3776 void add_identities(pset *value_table, ir_node *node) {
3777         if (get_opt_cse() && is_no_Block(node))
3778                 identify_remember(value_table, node);
3779 }  /* add_identities */
3780
3781 /* Visit each node in the value table of a graph. */
3782 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3783         ir_node *node;
3784         ir_graph *rem = current_ir_graph;
3785
3786         current_ir_graph = irg;
3787         foreach_pset(irg->value_table, node)
3788                 visit(node, env);
3789         current_ir_graph = rem;
3790 }  /* visit_all_identities */
3791
3792 /**
3793  * Garbage in, garbage out. If a node has a dead input, i.e., the
3794  * Bad node is input to the node, return the Bad node.
3795  */
3796 static INLINE ir_node *gigo(ir_node *node) {
3797         int i, irn_arity;
3798         ir_op *op = get_irn_op(node);
3799
3800         /* remove garbage blocks by looking at control flow that leaves the block
3801            and replacing the control flow by Bad. */
3802         if (get_irn_mode(node) == mode_X) {
3803                 ir_node *block = get_nodes_block(skip_Proj(node));
3804
3805                 /* Don't optimize nodes in immature blocks. */
3806                 if (!get_Block_matured(block)) return node;
3807                 /* Don't optimize End, may have Bads. */
3808                 if (op == op_End) return node;
3809
3810                 if (is_Block(block)) {
3811                         irn_arity = get_irn_arity(block);
3812                         for (i = 0; i < irn_arity; i++) {
3813                                 if (!is_Bad(get_irn_n(block, i)))
3814                                         break;
3815                         }
3816                         if (i == irn_arity) {
3817                                 ir_graph *irg = get_irn_irg(block);
3818                                 /* the start block is never dead */
3819                                 if (block != get_irg_start_block(irg)
3820                                         && block != get_irg_end_block(irg))
3821                                         return new_Bad();
3822                         }
3823                 }
3824         }
3825
3826         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3827            blocks predecessors is dead. */
3828         if (op != op_Block && op != op_Phi && op != op_Tuple) {
3829                 irn_arity = get_irn_arity(node);
3830
3831                 /*
3832                  * Beware: we can only read the block of a non-floating node.
3833                  */
3834                 if (is_irn_pinned_in_irg(node) &&
3835                         is_Block_dead(get_nodes_block(node)))
3836                         return new_Bad();
3837
3838                 for (i = 0; i < irn_arity; i++) {
3839                         ir_node *pred = get_irn_n(node, i);
3840
3841                         if (is_Bad(pred))
3842                                 return new_Bad();
3843 #if 0
3844                         /* Propagating Unknowns here seems to be a bad idea, because
3845                            sometimes we need a node as a input and did not want that
3846                            it kills it's user.
3847                            However, it might be useful to move this into a later phase
3848                            (if you think that optimizing such code is useful). */
3849                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3850                                 return new_Unknown(get_irn_mode(node));
3851 #endif
3852                 }
3853         }
3854 #if 0
3855         /* With this code we violate the agreement that local_optimize
3856            only leaves Bads in Block, Phi and Tuple nodes. */
3857         /* If Block has only Bads as predecessors it's garbage. */
3858         /* If Phi has only Bads as predecessors it's garbage. */
3859         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3860                 irn_arity = get_irn_arity(node);
3861                 for (i = 0; i < irn_arity; i++) {
3862                         if (!is_Bad(get_irn_n(node, i))) break;
3863                 }
3864                 if (i == irn_arity) node = new_Bad();
3865         }
3866 #endif
3867         return node;
3868 }  /* gigo */
3869
3870 /**
3871  * These optimizations deallocate nodes from the obstack.
3872  * It can only be called if it is guaranteed that no other nodes
3873  * reference this one, i.e., right after construction of a node.
3874  *
3875  * @param n   The node to optimize
3876  *
3877  * current_ir_graph must be set to the graph of the node!
3878  */
3879 ir_node *optimize_node(ir_node *n) {
3880         tarval *tv;
3881         ir_node *oldn = n;
3882         ir_opcode iro = get_irn_opcode(n);
3883
3884         /* Always optimize Phi nodes: part of the construction. */
3885         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3886
3887         /* constant expression evaluation / constant folding */
3888         if (get_opt_constant_folding()) {
3889                 /* neither constants nor Tuple values can be evaluated */
3890                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3891                         /* try to evaluate */
3892                         tv = computed_value(n);
3893                         if (tv != tarval_bad) {
3894                                 ir_node *nw;
3895                                 ir_type *old_tp = get_irn_type(n);
3896                                 int i, arity = get_irn_arity(n);
3897                                 int node_size;
3898
3899                                 /*
3900                                  * Try to recover the type of the new expression.
3901                                  */
3902                                 for (i = 0; i < arity && !old_tp; ++i)
3903                                         old_tp = get_irn_type(get_irn_n(n, i));
3904
3905                                 /*
3906                                  * we MUST copy the node here temporary, because it's still needed
3907                                  * for DBG_OPT_CSTEVAL
3908                                  */
3909                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3910                                 oldn = alloca(node_size);
3911
3912                                 memcpy(oldn, n, node_size);
3913                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3914
3915                                 /* ARG, copy the in array, we need it for statistics */
3916                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3917
3918                                 /* note the inplace edges module */
3919                                 edges_node_deleted(n, current_ir_graph);
3920
3921                                 /* evaluation was successful -- replace the node. */
3922                                 irg_kill_node(current_ir_graph, n);
3923                                 nw = new_Const(get_tarval_mode (tv), tv);
3924
3925                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3926                                         set_Const_type(nw, old_tp);
3927                                 DBG_OPT_CSTEVAL(oldn, nw);
3928                                 return nw;
3929                         }
3930                 }
3931         }
3932
3933         /* remove unnecessary nodes */
3934         if (get_opt_constant_folding() ||
3935             (iro == iro_Phi)  ||   /* always optimize these nodes. */
3936             (iro == iro_Id)   ||
3937             (iro == iro_Proj) ||
3938             (iro == iro_Block)  )  /* Flags tested local. */
3939                 n = equivalent_node(n);
3940
3941         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3942
3943         /* Common Subexpression Elimination.
3944          *
3945          * Checks whether n is already available.
3946          * The block input is used to distinguish different subexpressions. Right
3947          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3948          * subexpressions within a block.
3949          */
3950         if (get_opt_cse())
3951                 n = identify_cons(current_ir_graph->value_table, n);
3952
3953         if (n != oldn) {
3954                 edges_node_deleted(oldn, current_ir_graph);
3955
3956                 /* We found an existing, better node, so we can deallocate the old node. */
3957                 irg_kill_node(current_ir_graph, oldn);
3958                 return n;
3959         }
3960
3961         /* Some more constant expression evaluation that does not allow to
3962            free the node. */
3963         iro = get_irn_opcode(n);
3964         if (get_opt_constant_folding() ||
3965             (iro == iro_Cond) ||
3966             (iro == iro_Proj))     /* Flags tested local. */
3967                 n = transform_node(n);
3968
3969         /* Remove nodes with dead (Bad) input.
3970            Run always for transformation induced Bads. */
3971         n = gigo (n);
3972
3973         /* Now we have a legal, useful node. Enter it in hash table for CSE */
3974         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3975                 n = identify_remember(current_ir_graph->value_table, n);
3976         }
3977
3978         return n;
3979 }  /* optimize_node */
3980
3981
3982 /**
3983  * These optimizations never deallocate nodes (in place).  This can cause dead
3984  * nodes lying on the obstack.  Remove these by a dead node elimination,
3985  * i.e., a copying garbage collection.
3986  */
3987 ir_node *optimize_in_place_2(ir_node *n) {
3988         tarval *tv;
3989         ir_node *oldn = n;
3990         ir_opcode iro = get_irn_opcode(n);
3991
3992         if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3993
3994         /* constant expression evaluation / constant folding */
3995         if (get_opt_constant_folding()) {
3996                 /* neither constants nor Tuple values can be evaluated */
3997                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3998                         /* try to evaluate */
3999                         tv = computed_value(n);
4000                         if (tv != tarval_bad) {
4001                                 /* evaluation was successful -- replace the node. */
4002                                 ir_type *old_tp = get_irn_type(n);
4003                                 int i, arity = get_irn_arity(n);
4004
4005                                 /*
4006                                  * Try to recover the type of the new expression.
4007                                  */
4008                                 for (i = 0; i < arity && !old_tp; ++i)
4009                                         old_tp = get_irn_type(get_irn_n(n, i));
4010
4011                                 n = new_Const(get_tarval_mode(tv), tv);
4012
4013                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4014                                         set_Const_type(n, old_tp);
4015
4016                                 DBG_OPT_CSTEVAL(oldn, n);
4017                                 return n;
4018                         }
4019                 }
4020         }
4021
4022         /* remove unnecessary nodes */
4023         if (get_opt_constant_folding() ||
4024             (iro == iro_Phi)  ||   /* always optimize these nodes. */
4025             (iro == iro_Id)   ||   /* ... */
4026             (iro == iro_Proj) ||   /* ... */
4027             (iro == iro_Block)  )  /* Flags tested local. */
4028                 n = equivalent_node(n);
4029
4030         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
4031
4032         /** common subexpression elimination **/
4033         /* Checks whether n is already available. */
4034         /* The block input is used to distinguish different subexpressions.  Right
4035            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4036            subexpressions within a block. */
4037         if (get_opt_cse()) {
4038                 n = identify(current_ir_graph->value_table, n);
4039         }
4040
4041         /* Some more constant expression evaluation. */
4042         iro = get_irn_opcode(n);
4043         if (get_opt_constant_folding() ||
4044                 (iro == iro_Cond) ||
4045                 (iro == iro_Proj))     /* Flags tested local. */
4046                 n = transform_node(n);
4047
4048         /* Remove nodes with dead (Bad) input.
4049            Run always for transformation induced Bads.  */
4050         n = gigo(n);
4051
4052         /* Now we can verify the node, as it has no dead inputs any more. */
4053         irn_vrfy(n);
4054
4055         /* Now we have a legal, useful node. Enter it in hash table for cse.
4056            Blocks should be unique anyways.  (Except the successor of start:
4057            is cse with the start block!) */
4058         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4059                 n = identify_remember(current_ir_graph->value_table, n);
4060
4061         return n;
4062 }  /* optimize_in_place_2 */
4063
4064 /**
4065  * Wrapper for external use, set proper status bits after optimization.
4066  */
4067 ir_node *optimize_in_place(ir_node *n) {
4068         /* Handle graph state */
4069         assert(get_irg_phase_state(current_ir_graph) != phase_building);
4070
4071         if (get_opt_global_cse())
4072                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4073         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4074                 set_irg_outs_inconsistent(current_ir_graph);
4075
4076         /* FIXME: Maybe we could also test whether optimizing the node can
4077            change the control graph. */
4078         set_irg_doms_inconsistent(current_ir_graph);
4079         return optimize_in_place_2(n);
4080 }  /* optimize_in_place */
4081
4082 /*
4083  * Sets the default operation for an ir_ops.
4084  */
4085 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4086         ops = firm_set_default_computed_value(code, ops);
4087         ops = firm_set_default_equivalent_node(code, ops);
4088         ops = firm_set_default_transform_node(code, ops);
4089         ops = firm_set_default_node_cmp_attr(code, ops);
4090         ops = firm_set_default_get_type(code, ops);
4091         ops = firm_set_default_get_type_attr(code, ops);
4092         ops = firm_set_default_get_entity_attr(code, ops);
4093
4094         return ops;
4095 }  /* firm_set_default_operations */