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