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