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