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