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