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