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