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