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