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