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