ad965bd38e1fac9757bd3495c37646936099ed6f
[libfirm] / ir / ir / iropt.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   iropt --- optimizations intertwined with IR construction.
23  * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdbool.h>
30
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
34 #include "irmode_t.h"
35 #include "iropt_t.h"
36 #include "ircons_t.h"
37 #include "irgmod.h"
38 #include "irverify.h"
39 #include "iroptimize.h"
40 #include "tv_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
43 #include "irflag_t.h"
44 #include "irhooks.h"
45 #include "irarch.h"
46 #include "hashptr.h"
47 #include "opt_polymorphy.h"
48 #include "irtools.h"
49 #include "irhooks.h"
50 #include "array_t.h"
51 #include "vrp.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
54 #include "be.h"
55
56 /* Make types visible to allow most efficient access */
57 #include "entity_t.h"
58
59 /**
60  * Returns the tarval of a Const node or tarval_bad for all other nodes.
61  */
62 static ir_tarval *default_value_of(const ir_node *n)
63 {
64         if (is_Const(n))
65                 return get_Const_tarval(n); /* might return tarval_bad */
66         else
67                 return tarval_bad;
68 }
69
70 value_of_func value_of_ptr = default_value_of;
71
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
74 {
75         if (func != NULL)
76                 value_of_ptr = func;
77         else
78                 value_of_ptr = default_value_of;
79 }
80
81 /**
82  * Return the value of a Constant.
83  */
84 static ir_tarval *computed_value_Const(const ir_node *n)
85 {
86         return get_Const_tarval(n);
87 }  /* computed_value_Const */
88
89 /**
90  * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
91  */
92 static ir_tarval *computed_value_SymConst(const ir_node *n)
93 {
94         ir_type   *type;
95         ir_entity *ent;
96
97         switch (get_SymConst_kind(n)) {
98         case symconst_type_size:
99                 type = get_SymConst_type(n);
100                 if (get_type_state(type) == layout_fixed)
101                         return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
102                 break;
103         case symconst_type_align:
104                 type = get_SymConst_type(n);
105                 if (get_type_state(type) == layout_fixed)
106                         return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
107                 break;
108         case symconst_ofs_ent:
109                 ent  = get_SymConst_entity(n);
110                 type = get_entity_owner(ent);
111                 if (get_type_state(type) == layout_fixed)
112                         return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
113                 break;
114         default:
115                 break;
116         }
117         return tarval_bad;
118 }  /* computed_value_SymConst */
119
120 /**
121  * Return the value of an Add.
122  */
123 static ir_tarval *computed_value_Add(const ir_node *n)
124 {
125         ir_node *a = get_Add_left(n);
126         ir_node *b = get_Add_right(n);
127
128         ir_tarval *ta = value_of(a);
129         ir_tarval *tb = value_of(b);
130
131         if ((ta != tarval_bad) && (tb != tarval_bad))
132                 return tarval_add(ta, tb);
133
134         /* 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 }  /* computed_value_Add */
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 }  /* computed_value_Sub */
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 }  /* computed_value_Carry */
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 }  /* computed_value_Borrow */
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 }  /* computed_value_Minus */
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 }  /* computed_value_Mul */
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 }  /* computed_value_And */
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 }  /* computed_value_Or */
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 }  /* computed_value_Eor */
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 }  /* computed_value_Not */
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 }  /* computed_value_Shl */
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 }  /* computed_value_Shr */
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 }  /* computed_value_Shrs */
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 }  /* computed_value_Rotl */
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 }  /* computed_value_Conv */
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 }  /* computed_value_Mux */
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 }  /* computed_value_Confirm */
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 }  /* do_computed_value_Div */
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 }  /* do_computed_value_Mod */
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 }  /* computed_value_Proj_Div */
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 }  /* computed_value_Proj_Mod */
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 }  /* computed_value_Proj */
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 && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
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 }  /* computed_value */
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 }  /* firm_set_default_computed_value */
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 }  /* equivalent_node_neutral_zero */
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 }  /* equivalent_node_Add */
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 }  /* equivalent_node_left_zero */
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 }  /* equivalent_node_Sub */
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 }  /* equivalent_node_idempotent_unop */
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 }  /* equivalent_node_Mul */
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 }  /* equivalent_node_Or */
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 }  /* equivalent_node_And */
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 }  /* equivalent_node_Conv */
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 }  /* equivalent_node_Phi */
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 }  /* equivalent_node_Proj_Tuple */
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 }  /* equivalent_node_Proj_Div */
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 }  /* equivalent_node_Proj_CopyB */
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 }  /* equivalent_node_Proj_Bound */
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 }  /* equivalent_node_Proj */
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 }  /* equivalent_node_Id */
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 }  /* equivalent_node */
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 }  /* firm_set_default_equivalent_node */
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 }  /* is_const_Phi */
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 }  /* apply_binop_on_phi */
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 }  /* apply_binop_on_2_phis */
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 }  /* apply_unop_on_phi */
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 }  /* apply_conv_on_phi */
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 }  /* transform_node_AddSub */
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         vrp_attr *a_vrp, *b_vrp;
1988
1989         n = transform_node_AddSub(n);
1990
1991         a = get_Add_left(n);
1992         b = get_Add_right(n);
1993
1994         mode = get_irn_mode(n);
1995
1996         if (mode_is_reference(mode)) {
1997                 ir_mode *lmode = get_irn_mode(a);
1998
1999                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2000                         /* an Add(a, NULL) is a hidden Conv */
2001                         dbg_info *dbg = get_irn_dbg_info(n);
2002                         return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2003                 }
2004         }
2005
2006         HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2007
2008         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2009         if (mode_is_float(mode)) {
2010                 ir_graph *irg = get_irn_irg(n);
2011                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2012                         return n;
2013         }
2014
2015         if (mode_is_num(mode)) {
2016                 ir_graph *irg = get_irn_irg(n);
2017                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2018                 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2019                                 && a == b && mode_is_int(mode)) {
2020                         ir_node *block = get_nodes_block(n);
2021
2022                         n = new_rd_Mul(
2023                                 get_irn_dbg_info(n),
2024                                 block,
2025                                 a,
2026                                 new_r_Const_long(irg, mode, 2),
2027                                 mode);
2028                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2029                         return n;
2030                 }
2031                 if (is_Minus(a)) {
2032                         n = new_rd_Sub(
2033                                         get_irn_dbg_info(n),
2034                                         get_nodes_block(n),
2035                                         b,
2036                                         get_Minus_op(a),
2037                                         mode);
2038                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2039                         return n;
2040                 }
2041                 if (is_Minus(b)) {
2042                         n = new_rd_Sub(
2043                                         get_irn_dbg_info(n),
2044                                         get_nodes_block(n),
2045                                         a,
2046                                         get_Minus_op(b),
2047                                         mode);
2048                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2049                         return n;
2050                 }
2051                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2052                         /* Here we rely on constants be on the RIGHT side */
2053                         if (is_Not(a)) {
2054                                 ir_node *op = get_Not_op(a);
2055
2056                                 if (is_Const(b) && is_Const_one(b)) {
2057                                         /* ~x + 1 = -x */
2058                                         ir_node *blk = get_nodes_block(n);
2059                                         n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2060                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2061                                         return n;
2062                                 }
2063                         }
2064                 }
2065         }
2066
2067         a_vrp = vrp_get_info(a);
2068         b_vrp = vrp_get_info(b);
2069
2070         if (a_vrp && b_vrp) {
2071                 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2072
2073                 if (tarval_is_null(vrp_val)) {
2074                         dbg_info *dbgi  = get_irn_dbg_info(n);
2075                         return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2076                 }
2077         }
2078         return n;
2079 }  /* transform_node_Add */
2080
2081 /**
2082  * returns -cnst or NULL if impossible
2083  */
2084 static ir_node *const_negate(ir_node *cnst)
2085 {
2086         ir_tarval *tv    = tarval_neg(get_Const_tarval(cnst));
2087         dbg_info  *dbgi  = get_irn_dbg_info(cnst);
2088         ir_graph  *irg   = get_irn_irg(cnst);
2089         if (tv == tarval_bad) return NULL;
2090         return new_rd_Const(dbgi, irg, tv);
2091 }
2092
2093 /**
2094  * Do the AddSub optimization, then Transform
2095  *   Constant folding on Phi
2096  *   Sub(0,a)          -> Minus(a)
2097  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2098  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2099  *   Sub(Add(a, x), x) -> a
2100  *   Sub(x, Add(x, a)) -> -a
2101  *   Sub(x, Const)     -> Add(x, -Const)
2102  */
2103 static ir_node *transform_node_Sub(ir_node *n)
2104 {
2105         ir_mode *mode;
2106         ir_node *oldn = n;
2107         ir_node *a, *b, *c;
2108
2109         n = transform_node_AddSub(n);
2110
2111         a = get_Sub_left(n);
2112         b = get_Sub_right(n);
2113
2114         mode = get_irn_mode(n);
2115
2116         if (mode_is_int(mode)) {
2117                 ir_mode *lmode = get_irn_mode(a);
2118
2119                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2120                         /* a Sub(a, NULL) is a hidden Conv */
2121                         dbg_info *dbg = get_irn_dbg_info(n);
2122                         n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2123                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2124                         return n;
2125                 }
2126
2127                 if (mode == lmode                                     &&
2128                     get_mode_arithmetic(mode) == irma_twos_complement &&
2129                     is_Const(a)                                       &&
2130                     get_Const_tarval(a) == get_mode_minus_one(mode)) {
2131                         /* -1 - x -> ~x */
2132                         dbg_info *dbg = get_irn_dbg_info(n);
2133                         n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2134                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2135                         return n;
2136                 }
2137         }
2138
2139 restart:
2140         HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2141
2142         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2143         if (mode_is_float(mode)) {
2144                 ir_graph *irg = get_irn_irg(n);
2145                 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2146                         return n;
2147         }
2148
2149         if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2150                 /* a - C -> a + (-C) */
2151                 ir_node *cnst = const_negate(b);
2152                 if (cnst != NULL) {
2153                         ir_node  *block = get_nodes_block(n);
2154                         dbg_info *dbgi  = get_irn_dbg_info(n);
2155
2156                         n = new_rd_Add(dbgi, block, a, cnst, mode);
2157                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2158                         return n;
2159                 }
2160         }
2161
2162         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2163                 dbg_info *dbg   = get_irn_dbg_info(n);
2164                 ir_node  *block = get_nodes_block(n);
2165                 ir_node  *left  = get_Minus_op(a);
2166                 ir_node  *add   = new_rd_Add(dbg, block, left, b, mode);
2167
2168                 n = new_rd_Minus(dbg, block, add, mode);
2169                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2170                 return n;
2171         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2172                 dbg_info *dbg   = get_irn_dbg_info(n);
2173                 ir_node  *block = get_nodes_block(n);
2174                 ir_node  *right = get_Minus_op(b);
2175
2176                 n = new_rd_Add(dbg, block, a, right, mode);
2177                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2178                 return n;
2179         } else if (is_Sub(b)) {
2180                 /* a - (b - c) -> a + (c - b)
2181                  *             -> (a - b) + c iff (b - c) is a pointer */
2182                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2183                 ir_node  *s_left  = get_Sub_left(b);
2184                 ir_node  *s_right = get_Sub_right(b);
2185                 ir_mode  *s_mode  = get_irn_mode(b);
2186                 if (mode_is_reference(s_mode)) {
2187                         ir_node  *lowest_block = get_nodes_block(n); /* a and b are live here */
2188                         ir_node  *sub     = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2189                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2190
2191                         if (s_mode != mode)
2192                                 s_right = new_r_Conv(lowest_block, s_right, mode);
2193                         n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2194                 } else {
2195                         ir_node  *s_block = get_nodes_block(b);
2196                         ir_node  *sub     = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2197                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2198                         ir_node  *a_block = get_nodes_block(n);
2199
2200                         n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2201                 }
2202                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2203                 return n;
2204 #if 0
2205         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2206                 ir_node *m_right = get_Mul_right(b);
2207                 if (is_Const(m_right)) {
2208                         ir_node *cnst2 = const_negate(m_right);
2209                         if (cnst2 != NULL) {
2210                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2211                                 ir_node  *m_block = get_nodes_block(b);
2212                                 ir_node  *m_left  = get_Mul_left(b);
2213                                 ir_mode  *m_mode  = get_irn_mode(b);
2214                                 ir_node  *mul     = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2215                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2216                                 ir_node  *a_block = get_nodes_block(n);
2217
2218                                 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2219                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2220                                 return n;
2221                         }
2222                 }
2223 #endif
2224         }
2225
2226         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2227         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2228                 n = new_rd_Minus(
2229                                 get_irn_dbg_info(n),
2230                                 get_nodes_block(n),
2231                                 b,
2232                                 mode);
2233                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2234                 return n;
2235         }
2236         if (is_Add(a)) {
2237                 if (mode_wrap_around(mode)) {
2238                         ir_node *left  = get_Add_left(a);
2239                         ir_node *right = get_Add_right(a);
2240
2241                         /* FIXME: Does the Conv's work only for two complement or generally? */
2242                         if (left == b) {
2243                                 if (mode != get_irn_mode(right)) {
2244                                         /* This Sub is an effective Cast */
2245                                         right = new_r_Conv(get_nodes_block(n), right, mode);
2246                                 }
2247                                 n = right;
2248                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2249                                 return n;
2250                         } else if (right == b) {
2251                                 if (mode != get_irn_mode(left)) {
2252                                         /* This Sub is an effective Cast */
2253                                         left = new_r_Conv(get_nodes_block(n), left, mode);
2254                                 }
2255                                 n = left;
2256                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2257                                 return n;
2258                         }
2259                 }
2260         }
2261         if (is_Add(b)) {
2262                 if (mode_wrap_around(mode)) {
2263                         ir_node *left  = get_Add_left(b);
2264                         ir_node *right = get_Add_right(b);
2265
2266                         /* FIXME: Does the Conv's work only for two complement or generally? */
2267                         if (left == a) {
2268                                 ir_mode *r_mode = get_irn_mode(right);
2269
2270                                 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2271                                 if (mode != r_mode) {
2272                                         /* This Sub is an effective Cast */
2273                                         n = new_r_Conv(get_nodes_block(n), n, mode);
2274                                 }
2275                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2276                                 return n;
2277                         } else if (right == a) {
2278                                 ir_mode *l_mode = get_irn_mode(left);
2279
2280                                 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2281                                 if (mode != l_mode) {
2282                                         /* This Sub is an effective Cast */
2283                                         n = new_r_Conv(get_nodes_block(n), n, mode);
2284                                 }
2285                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2286                                 return n;
2287                         }
2288                 }
2289         }
2290         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2291                 ir_mode *mode = get_irn_mode(a);
2292
2293                 if (mode == get_irn_mode(b)) {
2294                         ir_mode *ma, *mb;
2295                         ir_node *op_a = get_Conv_op(a);
2296                         ir_node *op_b = get_Conv_op(b);
2297
2298                         /* check if it's allowed to skip the conv */
2299                         ma = get_irn_mode(op_a);
2300                         mb = get_irn_mode(op_b);
2301
2302                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2303                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2304                                 a = op_a; b = op_b;
2305                                 set_Sub_left(n, a);
2306                                 set_Sub_right(n, b);
2307
2308                                 goto restart;
2309                         }
2310                 }
2311         }
2312         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2313         if (!is_reassoc_running() && is_Mul(a)) {
2314                 ir_node *ma = get_Mul_left(a);
2315                 ir_node *mb = get_Mul_right(a);
2316
2317                 if (ma == b) {
2318                         ir_node  *blk = get_nodes_block(n);
2319                         ir_graph *irg = get_irn_irg(n);
2320                         n = new_rd_Mul(
2321                                         get_irn_dbg_info(n),
2322                                         blk,
2323                                         ma,
2324                                         new_rd_Sub(
2325                                                 get_irn_dbg_info(n),
2326                                                 blk,
2327                                                 mb,
2328                                                 new_r_Const(irg, get_mode_one(mode)),
2329                                                 mode),
2330                                         mode);
2331                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2332                         return n;
2333                 } else if (mb == b) {
2334                         ir_node  *blk = get_nodes_block(n);
2335                         ir_graph *irg = get_irn_irg(n);
2336                         n = new_rd_Mul(
2337                                         get_irn_dbg_info(n),
2338                                         blk,
2339                                         mb,
2340                                         new_rd_Sub(
2341                                                 get_irn_dbg_info(n),
2342                                                 blk,
2343                                                 ma,
2344                                                 new_r_Const(irg, get_mode_one(mode)),
2345                                                 mode),
2346                                         mode);
2347                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2348                         return n;
2349                 }
2350         }
2351         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2352                 ir_node *x        = get_Sub_left(a);
2353                 ir_node *y        = get_Sub_right(a);
2354                 ir_node *blk      = get_nodes_block(n);
2355                 ir_mode *m_b      = get_irn_mode(b);
2356                 ir_mode *m_y      = get_irn_mode(y);
2357                 ir_mode *add_mode;
2358                 ir_node *add;
2359
2360                 /* Determine the right mode for the Add. */
2361                 if (m_b == m_y)
2362                         add_mode = m_b;
2363                 else if (mode_is_reference(m_b))
2364                         add_mode = m_b;
2365                 else if (mode_is_reference(m_y))
2366                         add_mode = m_y;
2367                 else {
2368                         /*
2369                          * Both modes are different but none is reference,
2370                          * happens for instance in SubP(SubP(P, Iu), Is).
2371                          * We have two possibilities here: Cast or ignore.
2372                          * Currently we ignore this case.
2373                          */
2374                         return n;
2375                 }
2376
2377                 add = new_r_Add(blk, y, b, add_mode);
2378
2379                 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2380                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2381                 return n;
2382         }
2383
2384         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2385                 /* c - ~X = X + (c+1) */
2386                 if (is_Const(a) && is_Not(b)) {
2387                         ir_tarval *tv = get_Const_tarval(a);
2388
2389                         tv = tarval_add(tv, get_mode_one(mode));
2390                         if (tv != tarval_bad) {
2391                                 ir_node  *blk = get_nodes_block(n);
2392                                 ir_graph *irg = get_irn_irg(n);
2393                                 ir_node *c = new_r_Const(irg, tv);
2394                                 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2395                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2396                                 return n;
2397                         }
2398                 }
2399                 /* x-(x&y) = x & ~y */
2400                 if (is_And(b)) {
2401                         ir_node *and_left  = get_And_left(b);
2402                         ir_node *and_right = get_And_right(b);
2403                         if (and_right == a) {
2404                                 ir_node *tmp = and_left;
2405                                 and_left  = and_right;
2406                                 and_right = tmp;
2407                         }
2408                         if (and_left == a) {
2409                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2410                                 ir_node  *block = get_nodes_block(n);
2411                                 ir_mode  *mode  = get_irn_mode(n);
2412                                 ir_node  *notn  = new_rd_Not(dbgi, block, and_right, mode);
2413                                 ir_node  *and   = new_rd_And(dbgi, block, a, notn, mode);
2414                                 return and;
2415                         }
2416                 }
2417         }
2418         return n;
2419 }  /* transform_node_Sub */
2420
2421 /**
2422  * Several transformation done on n*n=2n bits mul.
2423  * These transformations must be done here because new nodes may be produced.
2424  */
2425 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2426 {
2427         ir_node   *oldn  = n;
2428         ir_node   *a     = get_Mul_left(n);
2429         ir_node   *b     = get_Mul_right(n);
2430         ir_tarval *ta    = value_of(a);
2431         ir_tarval *tb    = value_of(b);
2432         ir_mode   *smode = get_irn_mode(a);
2433
2434         if (ta == get_mode_one(smode)) {
2435                 /* (L)1 * (L)b = (L)b */
2436                 ir_node *blk = get_nodes_block(n);
2437                 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2438                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2439                 return n;
2440         }
2441         else if (ta == get_mode_minus_one(smode)) {
2442                 /* (L)-1 * (L)b = (L)b */
2443                 ir_node *blk = get_nodes_block(n);
2444                 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2445                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2446                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2447                 return n;
2448         }
2449         if (tb == get_mode_one(smode)) {
2450                 /* (L)a * (L)1 = (L)a */
2451                 ir_node *blk = get_irn_n(a, -1);
2452                 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2453                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2454                 return n;
2455         }
2456         else if (tb == get_mode_minus_one(smode)) {
2457                 /* (L)a * (L)-1 = (L)-a */
2458                 ir_node *blk = get_nodes_block(n);
2459                 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2460                 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2461                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2462                 return n;
2463         }
2464         return n;
2465 }
2466
2467 /**
2468  * Transform Mul(a,-1) into -a.
2469  * Do constant evaluation of Phi nodes.
2470  * Do architecture dependent optimizations on Mul nodes
2471  */
2472 static ir_node *transform_node_Mul(ir_node *n)
2473 {
2474         ir_node *c, *oldn = n;
2475         ir_mode *mode = get_irn_mode(n);
2476         ir_node *a = get_Mul_left(n);
2477         ir_node *b = get_Mul_right(n);
2478
2479         if (is_Bad(a) || is_Bad(b))
2480                 return n;
2481
2482         if (mode != get_irn_mode(a))
2483                 return transform_node_Mul2n(n, mode);
2484
2485         HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2486
2487         if (mode_is_signed(mode)) {
2488                 ir_node *r = NULL;
2489
2490                 if (value_of(a) == get_mode_minus_one(mode))
2491                         r = b;
2492                 else if (value_of(b) == get_mode_minus_one(mode))
2493                         r = a;
2494                 if (r) {
2495                         n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2496                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2497                         return n;
2498                 }
2499         }
2500         if (is_Minus(a)) {
2501                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2502                         ir_node *cnst = const_negate(b);
2503                         if (cnst != NULL) {
2504                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2505                                 ir_node  *block = get_nodes_block(n);
2506                                 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2507                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2508                                 return n;
2509                         }
2510                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2511                         dbg_info *dbgi  = get_irn_dbg_info(n);
2512                         ir_node  *block = get_nodes_block(n);
2513                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2514                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2515                         return n;
2516                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2517                         ir_node  *sub_l = get_Sub_left(b);
2518                         ir_node  *sub_r = get_Sub_right(b);
2519                         dbg_info *dbgi  = get_irn_dbg_info(n);
2520                         ir_node  *block = get_nodes_block(n);
2521                         ir_node  *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2522                         n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2523                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2524                         return n;
2525                 }
2526         } else if (is_Minus(b)) {
2527                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2528                         ir_node  *sub_l = get_Sub_left(a);
2529                         ir_node  *sub_r = get_Sub_right(a);
2530                         dbg_info *dbgi  = get_irn_dbg_info(n);
2531                         ir_node  *block = get_nodes_block(n);
2532                         ir_node  *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2533                         n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2534                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2535                         return n;
2536                 }
2537         } else if (is_Shl(a)) {
2538                 ir_node *const shl_l = get_Shl_left(a);
2539                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2540                         /* (1 << x) * b -> b << x */
2541                         dbg_info *const dbgi  = get_irn_dbg_info(n);
2542                         ir_node  *const block = get_nodes_block(n);
2543                         ir_node  *const shl_r = get_Shl_right(a);
2544                         n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2545                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2546                         return n;
2547                 }
2548         } else if (is_Shl(b)) {
2549                 ir_node *const shl_l = get_Shl_left(b);
2550                 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2551                         /* a * (1 << x) -> a << x */
2552                         dbg_info *const dbgi  = get_irn_dbg_info(n);
2553                         ir_node  *const block = get_nodes_block(n);
2554                         ir_node  *const shl_r = get_Shl_right(b);
2555                         n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2556                         // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2557                         return n;
2558                 }
2559         }
2560         if (get_mode_arithmetic(mode) == irma_ieee754) {
2561                 if (is_Const(a)) {
2562                         ir_tarval *tv = get_Const_tarval(a);
2563                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2564                                         && !tarval_is_negative(tv)) {
2565                                 /* 2.0 * b = b + b */
2566                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2567                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2568                                 return n;
2569                         }
2570                 }
2571                 else if (is_Const(b)) {
2572                         ir_tarval *tv = get_Const_tarval(b);
2573                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2574                                         && !tarval_is_negative(tv)) {
2575                                 /* a * 2.0 = a + a */
2576                                 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2577                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2578                                 return n;
2579                         }
2580                 }
2581         }
2582         return arch_dep_replace_mul_with_shifts(n);
2583 }  /* transform_node_Mul */
2584
2585 /**
2586  * Transform a Div Node.
2587  */
2588 static ir_node *transform_node_Div(ir_node *n)
2589 {
2590         ir_mode *mode = get_Div_resmode(n);
2591         ir_node *a = get_Div_left(n);
2592         ir_node *b = get_Div_right(n);
2593         ir_node *value = n;
2594         const ir_node *dummy;
2595
2596         if (mode_is_int(mode)) {
2597                 if (is_Const(b) && is_const_Phi(a)) {
2598                         /* check for Div(Phi, Const) */
2599                         value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2600                         if (value) {
2601                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2602                                 goto make_tuple;
2603                         }
2604                 } else if (is_Const(a) && is_const_Phi(b)) {
2605                         /* check for Div(Const, Phi) */
2606                         value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2607                         if (value) {
2608                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2609                                 goto make_tuple;
2610                         }
2611                 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2612                         /* check for Div(Phi, Phi) */
2613                         value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2614                         if (value) {
2615                                 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2616                                 goto make_tuple;
2617                         }
2618                 }
2619
2620                 if (a == b && value_not_zero(a, &dummy)) {
2621                         ir_graph *irg = get_irn_irg(n);
2622                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2623                         value = new_r_Const(irg, get_mode_one(mode));
2624                         DBG_OPT_CSTEVAL(n, value);
2625                         goto make_tuple;
2626                 } else {
2627                         if (mode_is_signed(mode) && is_Const(b)) {
2628                                 ir_tarval *tv = get_Const_tarval(b);
2629
2630                                 if (tv == get_mode_minus_one(mode)) {
2631                                         /* a / -1 */
2632                                         value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2633                                         DBG_OPT_CSTEVAL(n, value);
2634                                         goto make_tuple;
2635                                 }
2636                         }
2637                         /* Try architecture dependent optimization */
2638                         value = arch_dep_replace_div_by_const(n);
2639                 }
2640         } else {
2641                 assert(mode_is_float(mode));
2642
2643                 /* Optimize x/c to x*(1/c) */
2644                 if (get_mode_arithmetic(mode) == irma_ieee754) {
2645                         ir_tarval *tv = value_of(b);
2646
2647                         if (tv != tarval_bad) {
2648                                 int rem = tarval_fp_ops_enabled();
2649
2650                                 /*
2651                                  * Floating point constant folding might be disabled here to
2652                                  * prevent rounding.
2653                                  * However, as we check for exact result, doing it is safe.
2654                                  * Switch it on.
2655                                  */
2656                                 tarval_enable_fp_ops(1);
2657                                 tv = tarval_div(get_mode_one(mode), tv);
2658                                 tarval_enable_fp_ops(rem);
2659
2660                                 /* Do the transformation if the result is either exact or we are
2661                                    not using strict rules. */
2662                                 if (tv != tarval_bad &&
2663                                         (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2664                                         ir_node  *block = get_nodes_block(n);
2665                                         ir_graph *irg   = get_irn_irg(block);
2666                                         ir_node  *c     = new_r_Const(irg, tv);
2667                                         dbg_info *dbgi  = get_irn_dbg_info(n);
2668                                         value = new_rd_Mul(dbgi, block, a, c, mode);
2669
2670                                         goto make_tuple;
2671                                 }
2672                         }
2673                 }
2674         }
2675
2676         if (value != n) {
2677                 ir_node *mem, *blk;
2678                 ir_graph *irg;
2679
2680 make_tuple:
2681                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2682                 mem = get_Div_mem(n);
2683                 blk = get_nodes_block(n);
2684                 irg = get_irn_irg(blk);
2685
2686                 /* skip a potential Pin */
2687                 mem = skip_Pin(mem);
2688                 turn_into_tuple(n, pn_Div_max+1);
2689                 set_Tuple_pred(n, pn_Div_M,         mem);
2690                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2691                 set_Tuple_pred(n, pn_Div_X_except,  new_r_Bad(irg, mode_X));
2692                 set_Tuple_pred(n, pn_Div_res,       value);
2693         }
2694         return n;
2695 }  /* transform_node_Div */
2696
2697 /**
2698  * Transform a Mod node.
2699  */
2700 static ir_node *transform_node_Mod(ir_node *n)
2701 {
2702         ir_mode   *mode = get_Mod_resmode(n);
2703         ir_node   *a    = get_Mod_left(n);
2704         ir_node   *b    = get_Mod_right(n);
2705         ir_graph  *irg;
2706         ir_node   *value;
2707         ir_tarval *tv;
2708
2709         if (is_Const(b) && is_const_Phi(a)) {
2710                 /* check for Div(Phi, Const) */
2711                 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2712                 if (value) {
2713                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2714                         goto make_tuple;
2715                 }
2716         }
2717         else if (is_Const(a) && is_const_Phi(b)) {
2718                 /* check for Div(Const, Phi) */
2719                 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2720                 if (value) {
2721                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2722                         goto make_tuple;
2723                 }
2724         }
2725         else if (is_const_Phi(a) && is_const_Phi(b)) {
2726                 /* check for Div(Phi, Phi) */
2727                 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2728                 if (value) {
2729                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2730                         goto make_tuple;
2731                 }
2732         }
2733
2734         value = n;
2735         tv = value_of(n);
2736         irg = get_irn_irg(n);
2737         if (tv != tarval_bad) {
2738                 value = new_r_Const(irg, tv);
2739
2740                 DBG_OPT_CSTEVAL(n, value);
2741                 goto make_tuple;
2742         } else {
2743                 ir_node       *a = get_Mod_left(n);
2744                 ir_node       *b = get_Mod_right(n);
2745                 const ir_node *dummy;
2746
2747                 if (a == b && value_not_zero(a, &dummy)) {
2748                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2749                         value = new_r_Const(irg, get_mode_null(mode));
2750                         DBG_OPT_CSTEVAL(n, value);
2751                         goto make_tuple;
2752                 } else {
2753                         if (mode_is_signed(mode) && is_Const(b)) {
2754                                 ir_tarval *tv = get_Const_tarval(b);
2755
2756                                 if (tv == get_mode_minus_one(mode)) {
2757                                         /* a % -1 = 0 */
2758                                         value = new_r_Const(irg, get_mode_null(mode));
2759                                         DBG_OPT_CSTEVAL(n, value);
2760                                         goto make_tuple;
2761                                 }
2762                         }
2763                         /* Try architecture dependent optimization */
2764                         value = arch_dep_replace_mod_by_const(n);
2765                 }
2766         }
2767
2768         if (value != n) {
2769                 ir_node *mem, *blk;
2770                 ir_graph *irg;
2771
2772 make_tuple:
2773                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2774                 mem = get_Mod_mem(n);
2775                 blk = get_nodes_block(n);
2776                 irg = get_irn_irg(blk);
2777
2778                 /* skip a potential Pin */
2779                 mem = skip_Pin(mem);
2780                 turn_into_tuple(n, pn_Mod_max+1);
2781                 set_Tuple_pred(n, pn_Mod_M,         mem);
2782                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2783                 set_Tuple_pred(n, pn_Mod_X_except,  new_r_Bad(irg, mode_X));
2784                 set_Tuple_pred(n, pn_Mod_res,       value);
2785         }
2786         return n;
2787 }  /* transform_node_Mod */
2788
2789 /**
2790  * Transform a Cond node.
2791  *
2792  * Replace the Cond by a Jmp if it branches on a constant
2793  * condition.
2794  */
2795 static ir_node *transform_node_Cond(ir_node *n)
2796 {
2797
2798         ir_node   *a   = get_Cond_selector(n);
2799         ir_graph  *irg = get_irn_irg(n);
2800         ir_tarval *ta;
2801         ir_node   *jmp;
2802
2803         /* we need block info which is not available in floating irgs */
2804         if (get_irg_pinned(irg) == op_pin_state_floats)
2805                 return n;
2806
2807         /* we do not handle switches here */
2808         if (get_irn_mode(a) != mode_b)
2809                 return n;
2810
2811         ta = value_of(a);
2812         if (ta == tarval_bad && is_Cmp(a)) {
2813                 /* try again with a direct call to compute_cmp, as we don't care
2814                  * about the MODEB_LOWERED flag here */
2815                 ta = compute_cmp(a);
2816         }
2817
2818         if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
2819                 /* It's a boolean Cond, branching on a boolean constant.
2820                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2821                 ir_node *blk = get_nodes_block(n);
2822                 jmp = new_r_Jmp(blk);
2823                 turn_into_tuple(n, pn_Cond_max+1);
2824                 if (ta == tarval_b_true) {
2825                         set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2826                         set_Tuple_pred(n, pn_Cond_true, jmp);
2827                 } else {
2828                         set_Tuple_pred(n, pn_Cond_false, jmp);
2829                         set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2830                 }
2831                 /* We might generate an endless loop, so keep it alive. */
2832                 add_End_keepalive(get_irg_end(irg), blk);
2833                 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
2834         }
2835         return n;
2836 }  /* transform_node_Cond */
2837
2838 /**
2839  * Prototype of a recursive transform function
2840  * for bitwise distributive transformations.
2841  */
2842 typedef ir_node* (*recursive_transform)(ir_node *n);
2843
2844 /**
2845  * makes use of distributive laws for and, or, eor
2846  *     and(a OP c, b OP c) -> and(a, b) OP c
2847  * note, might return a different op than n
2848  */
2849 static ir_node *transform_bitwise_distributive(ir_node *n,
2850                                                recursive_transform trans_func)
2851 {
2852         ir_node *oldn    = n;
2853         ir_node *a       = get_binop_left(n);
2854         ir_node *b       = get_binop_right(n);
2855         ir_op   *op      = get_irn_op(a);
2856         ir_op   *op_root = get_irn_op(n);
2857
2858         if (op != get_irn_op(b))
2859                 return n;
2860
2861         /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2862         if (op == op_Conv) {
2863                 ir_node *a_op   = get_Conv_op(a);
2864                 ir_node *b_op   = get_Conv_op(b);
2865                 ir_mode *a_mode = get_irn_mode(a_op);
2866                 ir_mode *b_mode = get_irn_mode(b_op);
2867                 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2868                         ir_node *blk = get_nodes_block(n);
2869
2870                         n = exact_copy(n);
2871                         set_binop_left(n, a_op);
2872                         set_binop_right(n, b_op);
2873                         set_irn_mode(n, a_mode);
2874                         n = trans_func(n);
2875                         n = new_r_Conv(blk, n, get_irn_mode(oldn));
2876
2877                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2878                         return n;
2879                 }
2880         }
2881
2882         if (op == op_Eor) {
2883                 /* nothing to gain here */
2884                 return n;
2885         }
2886
2887         if (op == op_Shrs || op == op_Shr || op == op_Shl
2888                         || op == op_And || op == op_Or || op == op_Eor) {
2889                 ir_node *a_left  = get_binop_left(a);
2890                 ir_node *a_right = get_binop_right(a);
2891                 ir_node *b_left  = get_binop_left(b);
2892                 ir_node *b_right = get_binop_right(b);
2893                 ir_node *c       = NULL;
2894                 ir_node *op1     = NULL;
2895                 ir_node *op2     = NULL;
2896
2897                 if (is_op_commutative(op)) {
2898                         if (a_left == b_left) {
2899                                 c   = a_left;
2900                                 op1 = a_right;
2901                                 op2 = b_right;
2902                         } else if (a_left == b_right) {
2903                                 c   = a_left;
2904                                 op1 = a_right;
2905                                 op2 = b_left;
2906                         } else if (a_right == b_left) {
2907                                 c   = a_right;
2908                                 op1 = a_left;
2909                                 op2 = b_right;
2910                         }
2911                 }
2912                 if (a_right == b_right) {
2913                         c   = a_right;
2914                         op1 = a_left;
2915                         op2 = b_left;
2916                 }
2917
2918                 if (c != NULL) {
2919                         /* (a sop c) & (b sop c) => (a & b) sop c */
2920                         ir_node *blk = get_nodes_block(n);
2921
2922                         ir_node *new_n = exact_copy(n);
2923                         set_binop_left(new_n, op1);
2924                         set_binop_right(new_n, op2);
2925                         new_n = trans_func(new_n);
2926
2927                         if (op_root == op_Eor && op == op_Or) {
2928                                 dbg_info  *dbgi = get_irn_dbg_info(n);
2929                                 ir_mode   *mode = get_irn_mode(c);
2930
2931                                 c = new_rd_Not(dbgi, blk, c, mode);
2932                                 n = new_rd_And(dbgi, blk, new_n, c, mode);
2933                         } else {
2934                                 n = exact_copy(a);
2935                                 set_nodes_block(n, blk);
2936                                 set_binop_left(n, new_n);
2937                                 set_binop_right(n, c);
2938                                 add_identities(n);
2939                         }
2940
2941                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2942                         return n;
2943                 }
2944         }
2945
2946         return n;
2947 }
2948
2949 /**
2950  * Create a 0 constant of given mode.
2951  */
2952 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2953 {
2954         ir_tarval *tv   = get_mode_null(mode);
2955         ir_node   *cnst = new_r_Const(irg, tv);
2956
2957         return cnst;
2958 }
2959
2960 static bool is_shiftop(const ir_node *n)
2961 {
2962         return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2963 }
2964
2965 /**
2966  * normalisation: (x & c1) >> c2   to   (x >> c2) & (c1 >> c2)
2967  *  (we can use:
2968  *    - and, or, xor          instead of &
2969  *    - Shl, Shr, Shrs, rotl  instead of >>
2970  *    (with a special case for Or/Xor + Shrs)
2971  *
2972  * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
2973  */
2974 static ir_node *transform_node_shift_bitop(ir_node *n)
2975 {
2976         ir_graph  *irg   = get_irn_irg(n);
2977         ir_node   *right = get_binop_right(n);
2978         ir_mode   *mode  = get_irn_mode(n);
2979         ir_node   *left;
2980         ir_node   *bitop_left;
2981         ir_node   *bitop_right;
2982         ir_op     *op_left;
2983         ir_node   *block;
2984         dbg_info  *dbgi;
2985         ir_node   *new_shift;
2986         ir_node   *new_bitop;
2987         ir_node   *new_const;
2988         ir_tarval *tv1;
2989         ir_tarval *tv2;
2990         ir_tarval *tv_shift;
2991
2992         if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2993                 return n;
2994
2995         assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
2996
2997         if (!is_Const(right))
2998                 return n;
2999
3000         left    = get_binop_left(n);
3001         op_left = get_irn_op(left);
3002         if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3003                 return n;
3004
3005         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3006         if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3007                 /* TODO: test if sign bit is affectes */
3008                 return n;
3009         }
3010
3011         bitop_right = get_binop_right(left);
3012         if (!is_Const(bitop_right))
3013                 return n;
3014
3015         bitop_left = get_binop_left(left);
3016
3017         block = get_nodes_block(n);
3018         dbgi  = get_irn_dbg_info(n);
3019         tv1   = get_Const_tarval(bitop_right);
3020         tv2   = get_Const_tarval(right);
3021
3022         assert(get_tarval_mode(tv1) == mode);
3023
3024         if (is_Shl(n)) {
3025                 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3026                 tv_shift  = tarval_shl(tv1, tv2);
3027         } else if (is_Shr(n)) {
3028                 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3029                 tv_shift  = tarval_shr(tv1, tv2);
3030         } else if (is_Shrs(n)) {
3031                 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3032                 tv_shift  = tarval_shrs(tv1, tv2);
3033         } else {
3034                 assert(is_Rotl(n));
3035                 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3036                 tv_shift  = tarval_rotl(tv1, tv2);
3037         }
3038
3039         assert(get_tarval_mode(tv_shift) == mode);
3040         irg       = get_irn_irg(n);
3041         new_const = new_r_Const(irg, tv_shift);
3042
3043         if (op_left == op_And) {
3044                 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3045         } else if (op_left == op_Or) {
3046                 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3047         } else {
3048                 assert(op_left == op_Eor);
3049                 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3050         }
3051
3052         return new_bitop;
3053 }
3054
3055 /**
3056  * normalisation: (x >> c1) & c2   to   (x & (c2<<c1)) >> c1
3057  *  (we can use:
3058  *    - and, or, xor          instead of &
3059  *    - Shl, Shr, Shrs, rotl  instead of >>
3060  *    (with a special case for Or/Xor + Shrs)
3061  *
3062  * This normalisation is usually good for the backend since << C can often be
3063  * matched as address-mode.
3064  */
3065 static ir_node *transform_node_bitop_shift(ir_node *n)
3066 {
3067         ir_graph  *irg   = get_irn_irg(n);
3068         ir_node   *left  = get_binop_left(n);
3069         ir_node   *right = get_binop_right(n);
3070         ir_mode   *mode  = get_irn_mode(n);
3071         ir_node   *shift_left;
3072         ir_node   *shift_right;
3073         ir_node   *block;
3074         dbg_info  *dbg_bitop;
3075         dbg_info  *dbg_shift;
3076         ir_node   *new_bitop;
3077         ir_node   *new_shift;
3078         ir_node   *new_const;
3079         ir_tarval *tv1;
3080         ir_tarval *tv2;
3081         ir_tarval *tv_bitop;
3082
3083         if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3084                 return n;
3085
3086         assert(is_And(n) || is_Or(n) || is_Eor(n));
3087         if (!is_Const(right) || !is_shiftop(left))
3088                 return n;
3089
3090         shift_left  = get_binop_left(left);
3091         shift_right = get_binop_right(left);
3092         if (!is_Const(shift_right))
3093                 return n;
3094
3095         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3096         if (is_Shrs(left)) {
3097                 /* TODO this could be improved */
3098                 return n;
3099         }
3100
3101         irg       = get_irn_irg(n);
3102         block     = get_nodes_block(n);
3103         dbg_bitop = get_irn_dbg_info(n);
3104         dbg_shift = get_irn_dbg_info(left);
3105         tv1       = get_Const_tarval(shift_right);
3106         tv2       = get_Const_tarval(right);
3107         assert(get_tarval_mode(tv2) == mode);
3108
3109         if (is_Shl(left)) {
3110                 tv_bitop = tarval_shr(tv2, tv1);
3111         } else if (is_Shr(left)) {
3112                 if (is_Or(n) || is_Eor(n)) {
3113                         /*
3114                          * TODO this can be improved by checking whether
3115                          *      the left shift produces an overflow
3116                          */
3117                         return n;
3118                 }
3119                 tv_bitop = tarval_shl(tv2, tv1);
3120         } else {
3121                 assert(is_Rotl(left));
3122                 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
3123         }
3124         new_const = new_r_Const(irg, tv_bitop);
3125
3126         if (is_And(n)) {
3127                 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
3128         } else if (is_Or(n)) {
3129                 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
3130         } else {
3131                 assert(is_Eor(n));
3132                 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
3133         }
3134
3135         if (is_Shl(left)) {
3136                 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
3137         } else if (is_Shr(left)) {
3138                 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
3139         } else {
3140                 assert(is_Rotl(left));
3141                 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
3142         }
3143
3144         return new_shift;
3145 }
3146
3147 /**
3148  * Transform an And.
3149  */
3150 static ir_node *transform_node_And(ir_node *n)
3151 {
3152         ir_node *c, *oldn = n;
3153         ir_node *a = get_And_left(n);
3154         ir_node *b = get_And_right(n);
3155         ir_mode *mode;
3156         vrp_attr *a_vrp, *b_vrp;
3157
3158         if (is_Cmp(a) && is_Cmp(b)) {
3159                 ir_node    *a_left     = get_Cmp_left(a);
3160                 ir_node    *a_right    = get_Cmp_right(a);
3161                 ir_node    *b_left     = get_Cmp_left(b);
3162                 ir_node    *b_right    = get_Cmp_right(b);
3163                 ir_relation a_relation = get_Cmp_relation(a);
3164                 ir_relation b_relation = get_Cmp_relation(b);
3165                 /* we can combine the relations of two compares with the same
3166                  * operands */
3167                 if (a_left == b_left && b_left == b_right) {
3168                         dbg_info   *dbgi         = get_irn_dbg_info(n);
3169                         ir_node    *block        = get_nodes_block(n);
3170                         ir_relation new_relation = a_relation & b_relation;
3171                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3172                 }
3173                 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3174                 if (a_relation == b_relation && a_relation == ir_relation_equal
3175                     && !mode_is_float(get_irn_mode(a_left))
3176                     && !mode_is_float(get_irn_mode(b_left))) {
3177                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3178                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3179                                 ir_node  *block  = get_nodes_block(n);
3180                                 ir_mode  *a_mode = get_irn_mode(a_left);
3181                                 ir_mode  *b_mode = get_irn_mode(b_left);
3182                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3183                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3184                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
3185                                 ir_node  *or     = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3186                                 ir_graph *irg    = get_irn_irg(n);
3187                                 ir_node  *zero   = create_zero_const(irg, b_mode);
3188                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3189                         }
3190                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3191                                 dbg_info *dbgi   = get_irn_dbg_info(n);
3192                                 ir_node  *block  = get_nodes_block(n);
3193                                 ir_mode  *a_mode = get_irn_mode(a_left);
3194                                 ir_mode  *b_mode = get_irn_mode(b_left);
3195                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3196                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3197                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
3198                                 ir_node  *or     = new_rd_Or(dbgi, block, xora, conv, a_mode);
3199                                 ir_graph *irg    = get_irn_irg(n);
3200                                 ir_node  *zero   = create_zero_const(irg, a_mode);
3201                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3202                         }
3203                 }
3204         }
3205
3206         mode = get_irn_mode(n);
3207         HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3208
3209         if (is_Or(a)) {
3210                 if (is_Not(b)) {
3211                         ir_node *op = get_Not_op(b);
3212                         if (is_And(op)) {
3213                                 ir_node *ba = get_And_left(op);
3214                                 ir_node *bb = get_And_right(op);
3215
3216                                 /* it's enough to test the following cases due to normalization! */
3217                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3218                                         /* (a|b) & ~(a&b) = a^b */
3219                                         ir_node *block = get_nodes_block(n);
3220
3221                                         n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3222                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3223                                         return n;
3224                                 }
3225                         }
3226                 }
3227         }
3228         if (is_Or(b)) {
3229                 if (is_Not(a)) {
3230                         ir_node *op = get_Not_op(a);
3231                         if (is_And(op)) {
3232                                 ir_node *aa = get_And_left(op);
3233                                 ir_node *ab = get_And_right(op);
3234
3235                                 /* it's enough to test the following cases due to normalization! */
3236                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3237                                         /* (a|b) & ~(a&b) = a^b */
3238                                         ir_node *block = get_nodes_block(n);
3239
3240                                         n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3241                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3242                                         return n;
3243                                 }
3244                         }
3245                 }
3246         }
3247         if (is_Eor(a)) {
3248                 ir_node *al = get_Eor_left(a);
3249                 ir_node *ar = get_Eor_right(a);
3250
3251                 if (al == b) {
3252                         /* (b ^ a) & b -> ~a & b */
3253                         dbg_info *dbg  = get_irn_dbg_info(n);
3254                         ir_node *block = get_nodes_block(n);
3255
3256                         ar = new_rd_Not(dbg, block, ar, mode);
3257                         n  = new_rd_And(dbg, block, ar, b, mode);
3258                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3259                         return n;
3260                 }
3261                 if (ar == b) {
3262                         /* (a ^ b) & b -> ~a & b */
3263                         dbg_info *dbg  = get_irn_dbg_info(n);
3264                         ir_node *block = get_nodes_block(n);
3265
3266                         al = new_rd_Not(dbg, block, al, mode);
3267                         n  = new_rd_And(dbg, block, al, b, mode);
3268                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3269                         return n;
3270                 }
3271         }
3272         if (is_Eor(b)) {
3273                 ir_node *bl = get_Eor_left(b);
3274                 ir_node *br = get_Eor_right(b);
3275
3276                 if (bl == a) {
3277                         /* a & (a ^ b) -> a & ~b */
3278                         dbg_info *dbg  = get_irn_dbg_info(n);
3279                         ir_node *block = get_nodes_block(n);
3280
3281                         br = new_rd_Not(dbg, block, br, mode);
3282                         n  = new_rd_And(dbg, block, br, a, mode);
3283                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3284                         return n;
3285                 }
3286                 if (br == a) {
3287                         /* a & (b ^ a) -> a & ~b */
3288                         dbg_info *dbg  = get_irn_dbg_info(n);
3289                         ir_node *block = get_nodes_block(n);
3290
3291                         bl = new_rd_Not(dbg, block, bl, mode);
3292                         n  = new_rd_And(dbg, block, bl, a, mode);
3293                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3294                         return n;
3295                 }
3296         }
3297         if (is_Not(a) && is_Not(b)) {
3298                 /* ~a & ~b = ~(a|b) */
3299                 ir_node *block = get_nodes_block(n);
3300                 ir_mode *mode = get_irn_mode(n);
3301
3302                 a = get_Not_op(a);
3303                 b = get_Not_op(b);
3304                 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3305                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3306                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3307                 return n;
3308         }
3309
3310         b_vrp = vrp_get_info(b);
3311         if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3312                                                 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3313
3314                 return b;
3315
3316         }
3317
3318         a_vrp = vrp_get_info(a);
3319         if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3320                                                 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3321                 return a;
3322         }
3323
3324         n = transform_bitwise_distributive(n, transform_node_And);
3325         if (is_And(n))
3326                 n = transform_node_bitop_shift(n);
3327
3328         return n;
3329 }  /* transform_node_And */
3330
3331 /* the order of the values is important! */
3332 typedef enum const_class {
3333         const_const = 0,
3334         const_like  = 1,
3335         const_other = 2
3336 } const_class;
3337
3338 static const_class classify_const(const ir_node* n)
3339 {
3340         if (is_Const(n))         return const_const;
3341         if (is_irn_constlike(n)) return const_like;
3342         return const_other;
3343 }
3344
3345 /**
3346  * Determines whether r is more constlike or has a larger index (in that order)
3347  * than l.
3348  */
3349 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3350 {
3351         const const_class l_order = classify_const(l);
3352         const const_class r_order = classify_const(r);
3353         return
3354                 l_order > r_order ||
3355                 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3356 }
3357
3358 /**
3359  * Transform an Eor.
3360  */
3361 static ir_node *transform_node_Eor(ir_node *n)
3362 {
3363         ir_node *c, *oldn = n;
3364         ir_node *a = get_Eor_left(n);
3365         ir_node *b = get_Eor_right(n);
3366         ir_mode *mode = get_irn_mode(n);
3367
3368         /* we can combine the relations of two compares with the same operands */
3369         if (is_Cmp(a) && is_Cmp(b)) {
3370                 ir_node *a_left  = get_Cmp_left(a);
3371                 ir_node *a_right = get_Cmp_left(a);
3372                 ir_node *b_left  = get_Cmp_left(b);
3373                 ir_node *b_right = get_Cmp_right(b);
3374                 if (a_left == b_left && b_left == b_right) {
3375                         dbg_info   *dbgi         = get_irn_dbg_info(n);
3376                         ir_node    *block        = get_nodes_block(n);
3377                         ir_relation a_relation   = get_Cmp_relation(a);
3378                         ir_relation b_relation   = get_Cmp_relation(b);
3379                         ir_relation new_relation = a_relation ^ b_relation;
3380                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3381                 }
3382         }
3383
3384         HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3385
3386         /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3387         if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3388                 dbg_info *dbg      = get_irn_dbg_info(n);
3389                 ir_node  *block    = get_nodes_block(n);
3390                 ir_node  *new_not  = new_rd_Not(dbg, block, b, mode);
3391                 ir_node  *new_left = get_Not_op(a);
3392                 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3393                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3394                 return n;
3395         } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3396                 dbg_info *dbg       = get_irn_dbg_info(n);
3397                 ir_node  *block     = get_nodes_block(n);
3398                 ir_node  *new_not   = new_rd_Not(dbg, block, a, mode);
3399                 ir_node  *new_right = get_Not_op(b);
3400                 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3401                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3402                 return n;
3403         }
3404
3405         /* x ^ 1...1 -> ~1 */
3406         if (is_Const(b) && is_Const_all_one(b)) {
3407                 n = new_r_Not(get_nodes_block(n), a, mode);
3408                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3409                 return n;
3410         }
3411
3412         n = transform_bitwise_distributive(n, transform_node_Eor);
3413         if (is_Eor(n))
3414                 n = transform_node_bitop_shift(n);
3415
3416         return n;
3417 }  /* transform_node_Eor */
3418
3419 /**
3420  * Transform a Not.
3421  */
3422 static ir_node *transform_node_Not(ir_node *n)
3423 {
3424         ir_node *c, *oldn = n;
3425         ir_node *a    = get_Not_op(n);
3426         ir_mode *mode = get_irn_mode(n);
3427
3428         HANDLE_UNOP_PHI(tarval_not,a,c);
3429
3430         /* check for a boolean Not */
3431         if (is_Cmp(a)) {
3432                 dbg_info *dbgi  = get_irn_dbg_info(a);
3433                 ir_node  *block = get_nodes_block(a);
3434                 ir_relation relation = get_Cmp_relation(a);
3435                 relation = get_negated_relation(relation);
3436                 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3437                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3438                 return n;
3439         }
3440
3441         /* normalize ~(a ^ b) => a ^ ~b */
3442         if (is_Eor(a)) {
3443                 dbg_info *dbg       = get_irn_dbg_info(n);
3444                 ir_node  *block     = get_nodes_block(n);
3445                 ir_node  *eor_right = get_Eor_right(a);
3446                 ir_node  *eor_left  = get_Eor_left(a);
3447                 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3448                 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3449                 return n;
3450         }
3451
3452         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3453                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3454                         dbg_info *dbg   = get_irn_dbg_info(n);
3455                         ir_graph *irg   = get_irn_irg(n);
3456                         ir_node  *block = get_nodes_block(n);
3457                         ir_node  *add_l = get_Minus_op(a);
3458                         ir_node  *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3459                         n = new_rd_Add(dbg, block, add_l, add_r, mode);
3460                 } else if (is_Add(a)) {
3461                         ir_node *add_r = get_Add_right(a);
3462                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3463                                 /* ~(x + -1) = -x */
3464                                 ir_node *op = get_Add_left(a);
3465                                 ir_node *blk = get_nodes_block(n);
3466                                 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3467                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3468                         }
3469                 }
3470         }
3471         return n;
3472 }
3473
3474 /**
3475  * Transform a Minus.
3476  * Optimize:
3477  *   -(~x) = x + 1
3478  *   -(a-b) = b - a
3479  *   -(a >>u (size-1)) = a >>s (size-1)
3480  *   -(a >>s (size-1)) = a >>u (size-1)
3481  *   -(a * const) -> a * -const
3482  */
3483 static ir_node *transform_node_Minus(ir_node *n)
3484 {
3485         ir_node *c, *oldn = n;
3486         ir_node *a = get_Minus_op(n);
3487         ir_mode *mode;
3488
3489         HANDLE_UNOP_PHI(tarval_neg,a,c);
3490
3491         mode = get_irn_mode(a);
3492         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3493                 /* the following rules are only to twos-complement */
3494                 if (is_Not(a)) {
3495                         /* -(~x) = x + 1 */
3496                         ir_node   *op  = get_Not_op(a);
3497                         ir_tarval *tv  = get_mode_one(mode);
3498                         ir_node   *blk = get_nodes_block(n);
3499                         ir_graph  *irg = get_irn_irg(blk);
3500                         ir_node   *c   = new_r_Const(irg, tv);
3501                         n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3502                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3503                         return n;
3504                 }
3505                 if (is_Shr(a)) {
3506                         ir_node *c = get_Shr_right(a);
3507
3508                         if (is_Const(c)) {
3509                                 ir_tarval *tv = get_Const_tarval(c);
3510
3511                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3512                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3513                                         ir_node *v = get_Shr_left(a);
3514
3515                                         n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3516                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3517                                         return n;
3518                                 }
3519                         }
3520                 }
3521                 if (is_Shrs(a)) {
3522                         ir_node *c = get_Shrs_right(a);
3523
3524                         if (is_Const(c)) {
3525                                 ir_tarval *tv = get_Const_tarval(c);
3526
3527                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3528                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3529                                         ir_node *v = get_Shrs_left(a);
3530
3531                                         n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3532                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3533                                         return n;
3534                                 }
3535                         }
3536                 }
3537         }
3538         if (is_Sub(a)) {
3539                 /* - (a-b) = b - a */
3540                 ir_node *la  = get_Sub_left(a);
3541                 ir_node *ra  = get_Sub_right(a);
3542                 ir_node *blk = get_nodes_block(n);
3543
3544                 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3545                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3546                 return n;
3547         }
3548
3549         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3550                 ir_node   *mul_l = get_Mul_left(a);
3551                 ir_node   *mul_r = get_Mul_right(a);
3552                 ir_tarval *tv    = value_of(mul_r);
3553                 if (tv != tarval_bad) {
3554                         tv = tarval_neg(tv);
3555                         if (tv != tarval_bad) {
3556                                 ir_graph *irg   = get_irn_irg(n);
3557                                 ir_node  *cnst  = new_r_Const(irg, tv);
3558                                 dbg_info *dbg   = get_irn_dbg_info(a);
3559                                 ir_node  *block = get_nodes_block(a);
3560                                 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3561                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3562                                 return n;
3563                         }
3564                 }
3565         }
3566
3567         return n;
3568 }  /* transform_node_Minus */
3569
3570 /**
3571  * Transform a Proj(Load) with a non-null address.
3572  */
3573 static ir_node *transform_node_Proj_Load(ir_node *proj)
3574 {
3575         if (get_opt_ldst_only_null_ptr_exceptions()) {
3576                 if (get_irn_mode(proj) == mode_X) {
3577                         ir_node *load = get_Proj_pred(proj);
3578
3579                         /* get the Load address */
3580                         const ir_node *addr = get_Load_ptr(load);
3581                         const ir_node *confirm;
3582
3583                         if (value_not_null(addr, &confirm)) {
3584                                 if (confirm == NULL) {
3585                                         /* this node may float if it did not depend on a Confirm */
3586                                         set_irn_pinned(load, op_pin_state_floats);
3587                                 }
3588                                 if (get_Proj_proj(proj) == pn_Load_X_except) {
3589                                         ir_graph *irg = get_irn_irg(proj);
3590                                         DBG_OPT_EXC_REM(proj);
3591                                         return new_r_Bad(irg, mode_X);
3592                                 } else {
3593                                         ir_node *blk = get_nodes_block(load);
3594                                         return new_r_Jmp(blk);
3595                                 }
3596                         }
3597                 }
3598         }
3599         return proj;
3600 }  /* transform_node_Proj_Load */
3601
3602 /**
3603  * Transform a Proj(Store) with a non-null address.
3604  */
3605 static ir_node *transform_node_Proj_Store(ir_node *proj)
3606 {
3607         if (get_opt_ldst_only_null_ptr_exceptions()) {
3608                 if (get_irn_mode(proj) == mode_X) {
3609                         ir_node *store = get_Proj_pred(proj);
3610
3611                         /* get the load/store address */
3612                         const ir_node *addr = get_Store_ptr(store);
3613                         const ir_node *confirm;
3614
3615                         if (value_not_null(addr, &confirm)) {
3616                                 if (confirm == NULL) {
3617                                         /* this node may float if it did not depend on a Confirm */
3618                                         set_irn_pinned(store, op_pin_state_floats);
3619                                 }
3620                                 if (get_Proj_proj(proj) == pn_Store_X_except) {
3621                                         ir_graph *irg = get_irn_irg(proj);
3622                                         DBG_OPT_EXC_REM(proj);
3623                                         return new_r_Bad(irg, mode_X);
3624                                 } else {
3625                                         ir_node *blk = get_nodes_block(store);
3626                                         return new_r_Jmp(blk);
3627                                 }
3628                         }
3629                 }
3630         }
3631         return proj;
3632 }  /* transform_node_Proj_Store */
3633
3634 /**
3635  * Transform a Proj(Div) with a non-zero value.
3636  * Removes the exceptions and routes the memory to the NoMem node.
3637  */
3638 static ir_node *transform_node_Proj_Div(ir_node *proj)
3639 {
3640         ir_node *div = get_Proj_pred(proj);
3641         ir_node *b   = get_Div_right(div);
3642         ir_node *res, *new_mem;
3643         const ir_node *confirm;
3644         long proj_nr;
3645
3646         if (value_not_zero(b, &confirm)) {
3647                 /* div(x, y) && y != 0 */
3648                 if (confirm == NULL) {
3649                         /* we are sure we have a Const != 0 */
3650                         new_mem = get_Div_mem(div);
3651                         new_mem = skip_Pin(new_mem);
3652                         set_Div_mem(div, new_mem);
3653                         set_irn_pinned(div, op_pin_state_floats);
3654                 }
3655
3656                 proj_nr = get_Proj_proj(proj);
3657                 switch (proj_nr) {
3658                 case pn_Div_X_regular:
3659                         return new_r_Jmp(get_nodes_block(div));
3660
3661                 case pn_Div_X_except: {
3662                         ir_graph *irg = get_irn_irg(proj);
3663                         /* we found an exception handler, remove it */
3664                         DBG_OPT_EXC_REM(proj);
3665                         return new_r_Bad(irg, mode_X);
3666                 }
3667
3668                 case pn_Div_M: {
3669                         ir_graph *irg = get_irn_irg(proj);
3670                         res = get_Div_mem(div);
3671                         new_mem = get_irg_no_mem(irg);
3672
3673                         if (confirm) {
3674                                 /* This node can only float up to the Confirm block */
3675                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3676                         }
3677                         set_irn_pinned(div, op_pin_state_floats);
3678                         /* this is a Div without exception, we can remove the memory edge */
3679                         set_Div_mem(div, new_mem);
3680                         return res;
3681                 }
3682                 }
3683         }
3684         return proj;
3685 }  /* transform_node_Proj_Div */
3686
3687 /**
3688  * Transform a Proj(Mod) with a non-zero value.
3689  * Removes the exceptions and routes the memory to the NoMem node.
3690  */
3691 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3692 {
3693         ir_node *mod = get_Proj_pred(proj);
3694         ir_node *b   = get_Mod_right(mod);
3695         ir_node *res, *new_mem;
3696         const ir_node *confirm;
3697         long proj_nr;
3698
3699         if (value_not_zero(b, &confirm)) {
3700                 /* mod(x, y) && y != 0 */
3701                 proj_nr = get_Proj_proj(proj);
3702
3703                 if (confirm == NULL) {
3704                         /* we are sure we have a Const != 0 */
3705                         new_mem = get_Mod_mem(mod);
3706                         new_mem = skip_Pin(new_mem);
3707                         set_Mod_mem(mod, new_mem);
3708                         set_irn_pinned(mod, op_pin_state_floats);
3709                 }
3710
3711                 switch (proj_nr) {
3712
3713                 case pn_Mod_X_regular:
3714                         return new_r_Jmp(get_irn_n(mod, -1));
3715
3716                 case pn_Mod_X_except: {
3717                         ir_graph *irg = get_irn_irg(proj);
3718                         /* we found an exception handler, remove it */
3719                         DBG_OPT_EXC_REM(proj);
3720                         return new_r_Bad(irg, mode_X);
3721                 }
3722
3723                 case pn_Mod_M: {
3724                         ir_graph *irg = get_irn_irg(proj);
3725                         res = get_Mod_mem(mod);
3726                         new_mem = get_irg_no_mem(irg);
3727
3728                         if (confirm) {
3729                                 /* This node can only float up to the Confirm block */
3730                                 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3731                         }
3732                         /* this is a Mod without exception, we can remove the memory edge */
3733                         set_Mod_mem(mod, new_mem);
3734                         return res;
3735                 }
3736                 case pn_Mod_res:
3737                         if (get_Mod_left(mod) == b) {
3738                                 /* a % a = 0 if a != 0 */
3739                                 ir_graph *irg  = get_irn_irg(proj);
3740                                 ir_mode  *mode = get_irn_mode(proj);
3741                                 ir_node  *res  = new_r_Const(irg, get_mode_null(mode));
3742
3743                                 DBG_OPT_CSTEVAL(mod, res);
3744                                 return res;
3745                         }
3746                 }
3747         }
3748         return proj;
3749 }  /* transform_node_Proj_Mod */
3750
3751 /**
3752  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3753  */
3754 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3755 {
3756         ir_node *n = get_Proj_pred(proj);
3757         ir_node *b = get_Cond_selector(n);
3758
3759         if (mode_is_int(get_irn_mode(b))) {
3760                 ir_tarval *tb = value_of(b);
3761
3762                 if (tb != tarval_bad) {
3763                         /* we have a constant switch */
3764                         long num = get_Proj_proj(proj);
3765
3766                         if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3767                                 if (get_tarval_long(tb) == num) {
3768                                         /* Do NOT create a jump here, or we will have 2 control flow ops
3769                                          * in a block. This case is optimized away in optimize_cf(). */
3770                                         return proj;
3771                                 } else {
3772                                         ir_graph *irg = get_irn_irg(proj);
3773                                         /* this case will NEVER be taken, kill it */
3774                                         clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3775                                         return new_r_Bad(irg, mode_X);
3776                                 }
3777                         }
3778                 } else {
3779                         long num = get_Proj_proj(proj);
3780                         vrp_attr *b_vrp = vrp_get_info(b);
3781                         if (num != get_Cond_default_proj(n) && b_vrp) {
3782                                 /* Try handling with vrp data. We only remove dead parts. */
3783                                 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3784
3785                                 if (b_vrp->range_type == VRP_RANGE) {
3786                                         ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3787                                         ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3788
3789                                         if ((cmp_result & ir_relation_greater) == cmp_result
3790                                             && (cmp_result2 & ir_relation_less) == cmp_result2) {
3791                                                 ir_graph *irg = get_irn_irg(proj);
3792                                                 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3793                                                 return new_r_Bad(irg, mode_X);
3794                                         }
3795                                 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3796                                         ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3797                                         ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3798
3799                                         if ((cmp_result & ir_relation_less_equal) == cmp_result
3800                                              && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3801                                                 ir_graph *irg = get_irn_irg(proj);
3802                                                 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3803                                                 return new_r_Bad(irg, mode_X);
3804                                         }
3805                                 }
3806
3807                                 if (!(tarval_cmp(
3808                                                                 tarval_and( b_vrp->bits_set, tp),
3809                                                                 b_vrp->bits_set
3810                                                                 ) == ir_relation_equal)) {
3811                                         ir_graph *irg = get_irn_irg(proj);
3812                                         clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3813                                         return new_r_Bad(irg, mode_X);
3814                                 }
3815
3816                                 if (!(tarval_cmp(
3817                                                                 tarval_and(
3818                                                                         tarval_not(tp),
3819                                                                         tarval_not(b_vrp->bits_not_set)),
3820                                                                 tarval_not(b_vrp->bits_not_set))
3821                                                                  == ir_relation_equal)) {
3822                                         ir_graph *irg = get_irn_irg(proj);
3823                                         clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3824                                         return new_r_Bad(irg, mode_X);
3825                                 }
3826                         }
3827                 }
3828         }
3829         return proj;
3830 }
3831
3832 /**
3833  * return true if the operation returns a value with exactly 1 bit set
3834  */
3835 static bool is_single_bit(const ir_node *node)
3836 {
3837         /* a first implementation, could be extended with vrp and others... */
3838         if (is_Shl(node)) {
3839                 ir_node *shl_l  = get_Shl_left(node);
3840                 ir_mode *mode   = get_irn_mode(node);
3841                 int      modulo = get_mode_modulo_shift(mode);
3842                 /* this works if we shift a 1 and we have modulo shift */
3843                 if (is_Const(shl_l) && is_Const_one(shl_l)
3844                                 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3845                         return true;
3846                 }
3847         } else if (is_Const(node)) {
3848                 ir_tarval *tv = get_Const_tarval(node);
3849                 return tarval_is_single_bit(tv);
3850         }
3851         return false;
3852 }
3853
3854 /**
3855  * Normalizes and optimizes Cmp nodes.
3856  */
3857 static ir_node *transform_node_Cmp(ir_node *n)
3858 {
3859         ir_node    *left     = get_Cmp_left(n);
3860         ir_node    *right    = get_Cmp_right(n);
3861         ir_mode    *mode     = get_irn_mode(left);
3862         ir_tarval  *tv       = NULL;
3863         bool        changed  = false;
3864         bool        changedc = false;
3865         ir_relation relation = get_Cmp_relation(n);
3866         ir_relation possible = ir_get_possible_cmp_relations(left, right);
3867
3868         /* mask out impossible relations */
3869         ir_relation new_relation = relation & possible;
3870         if (new_relation != relation) {
3871                 relation = new_relation;
3872                 changed  = true;
3873         }
3874
3875         /* Remove unnecessary conversions */
3876         if (is_Conv(left) && is_Conv(right)) {
3877                 ir_node *op_left    = get_Conv_op(left);
3878                 ir_node *op_right   = get_Conv_op(right);
3879                 ir_mode *mode_left  = get_irn_mode(op_left);
3880                 ir_mode *mode_right = get_irn_mode(op_right);
3881
3882                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3883                                 && mode_left != mode_b && mode_right != mode_b) {
3884                         ir_node *block = get_nodes_block(n);
3885
3886                         if (mode_left == mode_right) {
3887                                 left    = op_left;
3888                                 right   = op_right;
3889                                 changed = true;
3890                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3891                         } else if (smaller_mode(mode_left, mode_right)) {
3892                                 left    = new_r_Conv(block, op_left, mode_right);
3893                                 right   = op_right;
3894                                 changed = true;
3895                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3896                         } else if (smaller_mode(mode_right, mode_left)) {
3897                                 left    = op_left;
3898                                 right   = new_r_Conv(block, op_right, mode_left);
3899                                 changed = true;
3900                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3901                         }
3902                         mode = get_irn_mode(left);
3903                 }
3904         }
3905         if (is_Conv(left) && is_Const(right)) {
3906                 ir_node   *op_left   = get_Conv_op(left);
3907                 ir_mode   *mode_left = get_irn_mode(op_left);
3908                 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3909                         ir_tarval *tv        = get_Const_tarval(right);
3910                         tarval_int_overflow_mode_t last_mode
3911                                 = tarval_get_integer_overflow_mode();
3912                         ir_tarval *new_tv;
3913                         tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3914                         new_tv = tarval_convert_to(tv, mode_left);
3915                         tarval_set_integer_overflow_mode(last_mode);
3916                         if (new_tv != tarval_bad) {
3917                                 ir_graph *irg = get_irn_irg(n);
3918                                 left    = op_left;
3919                                 right   = new_r_Const(irg, new_tv);
3920                                 mode    = get_irn_mode(left);
3921                                 changed = true;
3922                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3923                         }
3924                 }
3925         }
3926
3927         /*
3928          * Optimize -a CMP -b into b CMP a.
3929          * This works only for modes where unary Minus cannot Overflow.
3930          * Note that two-complement integers can Overflow so it will NOT work.
3931          */
3932         if (!mode_overflow_on_unary_Minus(mode) &&
3933                         is_Minus(left) && is_Minus(right)) {
3934                 left     = get_Minus_op(left);
3935                 right    = get_Minus_op(right);
3936                 relation = get_inversed_relation(relation);
3937                 changed  = true;
3938                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3939         }
3940
3941         /* remove operation on both sides if possible */
3942         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3943                 /*
3944                  * The following operations are NOT safe for floating point operations, for instance
3945                  * 1.0 + inf == 2.0 + inf, =/=> x == y
3946                  */
3947                 if (mode_is_int(mode)) {
3948                         unsigned lop = get_irn_opcode(left);
3949
3950                         if (lop == get_irn_opcode(right)) {
3951                                 ir_node *ll, *lr, *rl, *rr;
3952
3953                                 /* same operation on both sides, try to remove */
3954                                 switch (lop) {
3955                                 case iro_Not:
3956                                 case iro_Minus:
3957                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3958                                         left  = get_unop_op(left);
3959                                         right = get_unop_op(right);
3960                                         changed = true;
3961                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3962                                         break;
3963                                 case iro_Add:
3964                                         ll = get_Add_left(left);
3965                                         lr = get_Add_right(left);
3966                                         rl = get_Add_left(right);
3967                                         rr = get_Add_right(right);
3968
3969                                         if (ll == rl) {
3970                                                 /* X + a CMP X + b ==> a CMP b */
3971                                                 left  = lr;
3972                                                 right = rr;
3973                                                 changed = true;
3974                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3975                                         } else if (ll == rr) {
3976                                                 /* X + a CMP b + X ==> a CMP b */
3977                                                 left  = lr;
3978                                                 right = rl;
3979                                                 changed = true;
3980                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3981                                         } else if (lr == rl) {
3982                                                 /* a + X CMP X + b ==> a CMP b */
3983                                                 left  = ll;
3984                                                 right = rr;
3985                                                 changed = true;
3986                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3987                                         } else if (lr == rr) {
3988                                                 /* a + X CMP b + X ==> a CMP b */
3989                                                 left  = ll;
3990                                                 right = rl;
3991                                                 changed = true;
3992                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3993                                         }
3994                                         break;
3995                                 case iro_Sub:
3996                                         ll = get_Sub_left(left);
3997                                         lr = get_Sub_right(left);
3998                                         rl = get_Sub_left(right);
3999                                         rr = get_Sub_right(right);
4000
4001                                         if (ll == rl) {
4002                                                 /* X - a CMP X - b ==> a CMP b */
4003                                                 left  = lr;
4004                                                 right = rr;
4005                                                 changed = true;
4006                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4007                                         } else if (lr == rr) {
4008                                                 /* a - X CMP b - X ==> a CMP b */
4009                                                 left  = ll;
4010                                                 right = rl;
4011                                                 changed = true;
4012                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4013                                         }
4014                                         break;
4015                                 case iro_Rotl:
4016                                         if (get_Rotl_right(left) == get_Rotl_right(right)) {
4017                                                 /* a ROTL X CMP b ROTL X ==> a CMP b */
4018                                                 left  = get_Rotl_left(left);
4019                                                 right = get_Rotl_left(right);
4020                                                 changed = true;
4021                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4022                                         }
4023                                         break;
4024                                 default:
4025                                         break;
4026                                 }
4027                         }
4028
4029                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
4030                         if (is_Add(left) || is_Sub(left)) {
4031                                 ir_node *ll = get_binop_left(left);
4032                                 ir_node *lr = get_binop_right(left);
4033
4034                                 if (lr == right && is_Add(left)) {
4035                                         ir_node *tmp = ll;
4036                                         ll = lr;
4037                                         lr = tmp;
4038                                 }
4039                                 if (ll == right) {
4040                                         ir_graph *irg = get_irn_irg(n);
4041                                         left     = lr;
4042                                         right   = create_zero_const(irg, mode);
4043                                         changed = true;
4044                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4045                                 }
4046                         }
4047                         if (is_Add(right) || is_Sub(right)) {
4048                                 ir_node *rl = get_binop_left(right);
4049                                 ir_node *rr = get_binop_right(right);
4050
4051                                 if (rr == left && is_Add(right)) {
4052                                         ir_node *tmp = rl;
4053                                         rl = rr;
4054                                         rr = tmp;
4055                                 }
4056                                 if (rl == left) {
4057                                         ir_graph *irg = get_irn_irg(n);
4058                                         left     = rr;
4059                                         right   = create_zero_const(irg, mode);
4060                                         changed = true;
4061                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4062                                 }
4063                         }
4064
4065                         if (is_And(left) && is_Const(right)) {
4066                                 ir_node *ll = get_binop_left(left);
4067                                 ir_node *lr = get_binop_right(left);
4068                                 if (is_Shr(ll) && is_Const(lr)) {
4069                                         /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4070                                         ir_node *block = get_nodes_block(n);
4071                                         ir_mode *mode = get_irn_mode(left);
4072
4073                                         ir_node *llr = get_Shr_right(ll);
4074                                         if (is_Const(llr)) {
4075                                                 dbg_info *dbg = get_irn_dbg_info(left);
4076                                                 ir_graph *irg = get_irn_irg(left);
4077
4078                                                 ir_tarval *c1    = get_Const_tarval(llr);
4079                                                 ir_tarval *c2    = get_Const_tarval(lr);
4080                                                 ir_tarval *c3    = get_Const_tarval(right);
4081                                                 ir_tarval *mask  = tarval_shl(c2, c1);
4082                                                 ir_tarval *value = tarval_shl(c3, c1);
4083
4084                                                 left  = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4085                                                 right = new_r_Const(irg, value);
4086                                                 changed = true;
4087                                         }
4088                                 }
4089                         }
4090                         /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4091                          * cases */
4092                         if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4093                                 right = get_Eor_right(left);
4094                                 left  = get_Eor_left(left);
4095                                 changed = true;
4096                         }
4097                 }  /* mode_is_int(...) */
4098         }
4099
4100         /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4101          * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4102         if (mode_is_int(mode) && is_And(left)
4103             && (relation == ir_relation_equal
4104                 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4105                 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4106                 ir_node *and0 = get_And_left(left);
4107                 ir_node *and1 = get_And_right(left);
4108                 if (and1 == right) {
4109                         ir_node *tmp = and0;
4110                         and0 = and1;
4111                         and1 = tmp;
4112                 }
4113                 if (and0 == right && is_single_bit(and0)) {
4114                         ir_graph *irg = get_irn_irg(n);
4115                         relation =
4116                                 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4117                         right = create_zero_const(irg, mode);
4118                         changed |= 1;
4119                 }
4120         }
4121
4122         /* replace mode_b compares with ands/ors */
4123         if (mode == mode_b) {
4124                 ir_node  *block = get_nodes_block(n);
4125                 ir_node  *bres;
4126
4127                 switch (relation) {
4128                         case ir_relation_less_equal:
4129                                 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4130                                 break;
4131                         case ir_relation_less:
4132                                 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4133                                 break;
4134                         case ir_relation_greater_equal:
4135                                 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4136                                 break;
4137                         case ir_relation_greater:
4138                                 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4139                                 break;
4140                         case ir_relation_less_greater:
4141                                 bres = new_r_Eor(block, left, right, mode_b);
4142                                 break;
4143                         case ir_relation_equal:
4144                                 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4145                                 break;
4146                         default:
4147 #ifdef DEBUG_libfirm
4148                                 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4149 #endif
4150                                 bres = NULL;
4151                 }
4152                 if (bres != NULL) {
4153                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4154                         return bres;
4155                 }
4156         }
4157
4158         /*
4159          * First step: normalize the compare op
4160          * by placing the constant on the right side
4161          * or moving the lower address node to the left.
4162          */
4163         if (!operands_are_normalized(left, right)) {
4164                 ir_node *t = left;
4165                 left  = right;
4166                 right = t;
4167
4168                 relation = get_inversed_relation(relation);
4169                 changed  = true;
4170         }
4171
4172         /*
4173          * Second step: Try to reduce the magnitude
4174          * of a constant. This may help to generate better code
4175          * later and may help to normalize more compares.
4176          * Of course this is only possible for integer values.
4177          */
4178         tv = value_of(right);
4179         if (tv != tarval_bad) {
4180                 ir_mode *mode = get_irn_mode(right);
4181
4182                 /* TODO extend to arbitrary constants */
4183                 if (is_Conv(left) && tarval_is_null(tv)) {
4184                         ir_node *op      = get_Conv_op(left);
4185                         ir_mode *op_mode = get_irn_mode(op);
4186
4187                         /*
4188                          * UpConv(x) REL 0  ==> x REL 0
4189                          * Don't do this for float values as it's unclear whether it is a
4190                          * win. (on the other side it makes detection/creation of fabs hard)
4191                          */
4192                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4193                             ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4194                                  mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4195                                 !mode_is_float(mode)) {
4196                                 tv   = get_mode_null(op_mode);
4197                                 left = op;
4198                                 mode = op_mode;
4199                                 changedc = true;
4200                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4201                         }
4202                 }
4203
4204                 if (tv != tarval_bad) {
4205                         /* the following optimization is possible on modes without Overflow
4206                          * on Unary Minus or on == and !=:
4207                          * -a CMP c  ==>  a swap(CMP) -c
4208                          *
4209                          * Beware: for two-complement Overflow may occur, so only == and != can
4210                          * be optimized, see this:
4211                          * -MININT < 0 =/=> MININT > 0 !!!
4212                          */
4213                         if (is_Minus(left) &&
4214                                 (!mode_overflow_on_unary_Minus(mode) ||
4215                                 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4216                                 tv = tarval_neg(tv);
4217
4218                                 if (tv != tarval_bad) {
4219                                         left = get_Minus_op(left);
4220                                         relation = get_inversed_relation(relation);
4221                                         changedc = true;
4222                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4223                                 }
4224                         } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4225                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
4226                                 tv = tarval_not(tv);
4227
4228                                 if (tv != tarval_bad) {
4229                                         left = get_Not_op(left);
4230                                         changedc = true;
4231                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4232                                 }
4233                         }
4234
4235                         /* for integer modes, we have more */
4236                         if (mode_is_int(mode) && !is_Const(left)) {
4237                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
4238                                 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4239                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4240                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4241
4242                                         if (tv != tarval_bad) {
4243                                                 relation ^= ir_relation_equal;
4244                                                 changedc = true;
4245                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4246                                         }
4247                                 }
4248                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4249                                 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4250                                         tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4251                                         tv = tarval_add(tv, get_mode_one(mode));
4252
4253                                         if (tv != tarval_bad) {
4254                                                 relation ^= ir_relation_equal;
4255                                                 changedc = true;
4256                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4257                                         }
4258                                 }
4259
4260                                 /* the following reassociations work only for == and != */
4261                                 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4262                                         if (tv != tarval_bad) {
4263                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4264                                                 if (is_Sub(left)) {
4265                                                         ir_node *c1 = get_Sub_right(left);
4266                                                         ir_tarval *tv2 = value_of(c1);
4267
4268                                                         if (tv2 != tarval_bad) {
4269                                                                 tv2 = tarval_add(tv, value_of(c1));
4270
4271                                                                 if (tv2 != tarval_bad) {
4272                                                                         left    = get_Sub_left(left);
4273                                                                         tv      = tv2;
4274                                                                         changedc = true;
4275                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4276                                                                 }
4277                                                         }
4278                                                 }
4279                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4280                                                 else if (is_Add(left)) {
4281                                                         ir_node *a_l = get_Add_left(left);
4282                                                         ir_node *a_r = get_Add_right(left);
4283                                                         ir_node *a;
4284                                                         ir_tarval *tv2;
4285
4286                                                         if (is_Const(a_l)) {
4287                                                                 a = a_r;
4288                                                                 tv2 = value_of(a_l);
4289                                                         } else {
4290                                                                 a = a_l;
4291                                                                 tv2 = value_of(a_r);
4292                                                         }
4293
4294                                                         if (tv2 != tarval_bad) {
4295                                                                 tv2 = tarval_sub(tv, tv2, NULL);
4296
4297                                                                 if (tv2 != tarval_bad) {
4298                                                                         left    = a;
4299                                                                         tv      = tv2;
4300                                                                         changedc = true;
4301                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4302                                                                 }
4303                                                         }
4304                                                 }
4305                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4306                                                 else if (is_Minus(left)) {
4307                                                         ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4308
4309                                                         if (tv2 != tarval_bad) {
4310                                                                 left    = get_Minus_op(left);
4311                                                                 tv      = tv2;
4312                                                                 changedc = true;
4313                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4314                                                         }
4315                                                 }
4316                                         }
4317                                 } /* == or != */
4318                         } /* mode_is_int */
4319
4320                         if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4321                                 switch (get_irn_opcode(left)) {
4322                                         ir_node *c1;
4323
4324                                 case iro_And:
4325                                         c1 = get_And_right(left);
4326                                         if (is_Const(c1)) {
4327                                                 /*
4328                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4329                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4330                                                  */
4331                                                 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4332                                                 if (mask != tv) {
4333                                                         /* TODO: move to constant evaluation */
4334                                                         ir_graph *irg = get_irn_irg(n);
4335                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4336                                                         c1 = new_r_Const(irg, tv);
4337                                                         DBG_OPT_CSTEVAL(n, c1);
4338                                                         return c1;
4339                                                 }
4340
4341                                                 if (tarval_is_single_bit(tv)) {
4342                                                         /*
4343                                                          * optimization for AND:
4344                                                          * Optimize:
4345                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4346                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4347                                                          *
4348                                                          * if C is a single Bit constant.
4349                                                          */
4350
4351                                                         /* check for Constant's match. We have check hare the tarvals,
4352                                                            because our const might be changed */
4353                                                         if (get_Const_tarval(c1) == tv) {
4354                                                                 /* fine: do the transformation */
4355                                                                 tv = get_mode_null(get_tarval_mode(tv));
4356                                                                 relation ^= ir_relation_less_equal_greater;
4357                                                                 changedc = true;
4358                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4359                                                         }
4360                                                 }
4361                                         }
4362                                         break;
4363                                 case iro_Or:
4364                                         c1 = get_Or_right(left);
4365                                         if (is_Const(c1) && tarval_is_null(tv)) {
4366                                                 /*
4367                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4368                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4369                                                  */
4370                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4371                                                         /* TODO: move to constant evaluation */
4372                                                         ir_graph *irg = get_irn_irg(n);
4373                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4374                                                         c1 = new_r_Const(irg, tv);
4375                                                         DBG_OPT_CSTEVAL(n, c1);
4376                                                         return c1;
4377                                                 }
4378                                         }
4379                                         break;
4380                                 case iro_Shl:
4381                                         /*
4382                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4383                                          *                             FALSE                       else
4384                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4385                                          *                             TRUE                        else
4386                                          */
4387                                         c1 = get_Shl_right(left);
4388                                         if (is_Const(c1)) {
4389                                                 ir_graph  *irg    = get_irn_irg(c1);
4390                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4391                                                 ir_mode   *mode   = get_irn_mode(left);
4392                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4393                                                 ir_tarval *amask  = tarval_shr(minus1, tv1);
4394                                                 ir_tarval *cmask  = tarval_shl(minus1, tv1);
4395                                                 ir_node   *sl, *blk;
4396
4397                                                 if (tarval_and(tv, cmask) != tv) {
4398                                                         /* condition not met */
4399                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4400                                                         c1 = new_r_Const(irg, tv);
4401                                                         DBG_OPT_CSTEVAL(n, c1);
4402                                                         return c1;
4403                                                 }
4404                                                 sl   = get_Shl_left(left);
4405                                                 blk  = get_nodes_block(n);
4406                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4407                                                 tv   = tarval_shr(tv, tv1);
4408                                                 changedc = true;
4409                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4410                                         }
4411                                         break;
4412                                 case iro_Shr:
4413                                         /*
4414                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4415                                          *                             FALSE                       else
4416                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4417                                          *                             TRUE                        else
4418                                          */
4419                                         c1 = get_Shr_right(left);
4420                                         if (is_Const(c1)) {
4421                                                 ir_graph  *irg    = get_irn_irg(c1);
4422                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4423                                                 ir_mode   *mode   = get_irn_mode(left);
4424                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4425                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4426                                                 ir_tarval *cmask  = tarval_shr(minus1, tv1);
4427                                                 ir_node   *sl, *blk;
4428
4429                                                 if (tarval_and(tv, cmask) != tv) {
4430                                                         /* condition not met */
4431                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4432                                                         c1 = new_r_Const(irg, tv);
4433                                                         DBG_OPT_CSTEVAL(n, c1);
4434                                                         return c1;
4435                                                 }
4436                                                 sl   = get_Shr_left(left);
4437                                                 blk  = get_nodes_block(n);
4438                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4439                                                 tv   = tarval_shl(tv, tv1);
4440                                                 changedc = true;
4441                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4442                                         }
4443                                         break;
4444                                 case iro_Shrs:
4445                                         /*
4446                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4447                                          *                             FALSE                       else
4448                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4449                                          *                             TRUE                        else
4450                                          */
4451                                         c1 = get_Shrs_right(left);
4452                                         if (is_Const(c1)) {
4453                                                 ir_graph  *irg    = get_irn_irg(c1);
4454                                                 ir_tarval *tv1    = get_Const_tarval(c1);
4455                                                 ir_mode   *mode   = get_irn_mode(left);
4456                                                 ir_tarval *minus1 = get_mode_all_one(mode);
4457                                                 ir_tarval *amask  = tarval_shl(minus1, tv1);
4458                                                 ir_tarval *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4459                                                 ir_node *sl, *blk;
4460
4461                                                 cond = tarval_sub(cond, tv1, NULL);
4462                                                 cond = tarval_shrs(tv, cond);
4463
4464                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4465                                                         /* condition not met */
4466                                                         tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4467                                                         c1 = new_r_Const(irg, tv);
4468                                                         DBG_OPT_CSTEVAL(n, c1);
4469                                                         return c1;
4470                                                 }
4471                                                 sl   = get_Shrs_left(left);
4472                                                 blk  = get_nodes_block(n);
4473                                                 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4474                                                 tv   = tarval_shl(tv, tv1);
4475                                                 changedc = true;
4476                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4477                                         }
4478                                         break;
4479                                 }  /* switch */
4480                         }
4481                 } /* tarval != bad */
4482         }
4483
4484         if (changedc) {     /* need a new Const */
4485                 ir_graph *irg = get_irn_irg(n);
4486                 right = new_r_Const(irg, tv);
4487                 changed = true;
4488         }
4489
4490         if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4491                 ir_node *op = get_Proj_pred(left);
4492
4493                 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4494                         ir_node *c = get_binop_right(op);
4495
4496                         if (is_Const(c)) {
4497                                 ir_tarval *tv = get_Const_tarval(c);
4498
4499                                 if (tarval_is_single_bit(tv)) {
4500                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4501                                         ir_node *v    = get_binop_left(op);
4502                                         ir_node *blk  = get_irn_n(op, -1);
4503                                         ir_graph *irg = get_irn_irg(op);
4504                                         ir_mode *mode = get_irn_mode(v);
4505
4506                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4507                                         left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4508                                         changed = true;
4509                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4510                                 }
4511                         }
4512                 }
4513         }
4514
4515         if (changed) {
4516                 dbg_info *dbgi  = get_irn_dbg_info(n);
4517                 ir_node  *block = get_nodes_block(n);
4518
4519                 /* create a new compare */
4520                 n = new_rd_Cmp(dbgi, block, left, right, relation);
4521         }
4522
4523         return n;
4524 }
4525
4526 /**
4527  * Optimize CopyB(mem, x, x) into a Nop.
4528  */
4529 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4530 {
4531         ir_node *copyb = get_Proj_pred(proj);
4532         ir_node *a     = get_CopyB_dst(copyb);
4533         ir_node *b     = get_CopyB_src(copyb);
4534
4535         if (a == b) {
4536                 switch (get_Proj_proj(proj)) {
4537                 case pn_CopyB_X_regular:
4538                         /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4539                         DBG_OPT_EXC_REM(proj);
4540                         proj = new_r_Jmp(get_nodes_block(copyb));
4541                         break;
4542                 case pn_CopyB_X_except: {
4543                         ir_graph *irg = get_irn_irg(proj);
4544                         DBG_OPT_EXC_REM(proj);
4545                         proj = new_r_Bad(irg, mode_X);
4546                         break;
4547                 }
4548                 default:
4549                         break;
4550                 }
4551         }
4552         return proj;
4553 }  /* transform_node_Proj_CopyB */
4554
4555 /**
4556  * Optimize Bounds(idx, idx, upper) into idx.
4557  */
4558 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4559 {
4560         ir_node *oldn  = proj;
4561         ir_node *bound = get_Proj_pred(proj);
4562         ir_node *idx   = get_Bound_index(bound);
4563         ir_node *pred  = skip_Proj(idx);
4564         int ret_tuple  = 0;
4565
4566         if (idx == get_Bound_lower(bound))
4567                 ret_tuple = 1;
4568         else if (is_Bound(pred)) {
4569                 /*
4570                 * idx was Bounds checked previously, it is still valid if
4571                 * lower <= pred_lower && pred_upper <= upper.
4572                 */
4573                 ir_node *lower = get_Bound_lower(bound);
4574                 ir_node *upper = get_Bound_upper(bound);
4575                 if (get_Bound_lower(pred) == lower &&
4576                         get_Bound_upper(pred) == upper) {
4577                         /*
4578                          * One could expect that we simply return the previous
4579                          * Bound here. However, this would be wrong, as we could
4580                          * add an exception Proj to a new location then.
4581                          * So, we must turn in into a tuple.
4582                          */
4583                         ret_tuple = 1;
4584                 }
4585         }
4586         if (ret_tuple) {
4587                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4588                 switch (get_Proj_proj(proj)) {
4589                 case pn_Bound_M:
4590                         DBG_OPT_EXC_REM(proj);
4591                         proj = get_Bound_mem(bound);
4592                         break;
4593                 case pn_Bound_X_except:
4594                         DBG_OPT_EXC_REM(proj);
4595                         proj = new_r_Bad(get_irn_irg(proj), mode_X);
4596                         break;
4597                 case pn_Bound_res:
4598                         proj = idx;
4599                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4600                         break;
4601                 case pn_Bound_X_regular:
4602                         DBG_OPT_EXC_REM(proj);
4603                         proj = new_r_Jmp(get_nodes_block(bound));
4604                         break;
4605                 default:
4606                         break;
4607                 }
4608         }
4609         return proj;
4610 }  /* transform_node_Proj_Bound */
4611
4612 /**
4613  * Does all optimizations on nodes that must be done on its Projs
4614  * because of creating new nodes.
4615  */
4616 static ir_node *transform_node_Proj(ir_node *proj)
4617 {
4618         ir_node *n = get_Proj_pred(proj);
4619
4620         if (n->op->ops.transform_node_Proj)
4621                 return n->op->ops.transform_node_Proj(proj);
4622         return proj;
4623 }  /* transform_node_Proj */
4624
4625 /**
4626  * Test wether a block is unreachable
4627  * Note: That this only returns true when
4628  * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
4629  * This is important, as you easily end up producing invalid constructs in the
4630  * unreachable code when optimizing away edges into the unreachable code.
4631  * So only set this flag when you iterate localopts to the fixpoint.
4632  * When you reach the fixpoint then all unreachable code is dead
4633  * (= can't be reached by firm edges) and you won't see the invalid constructs
4634  * anymore.
4635  */
4636 static bool is_block_unreachable(const ir_node *block)
4637 {
4638         const ir_graph *irg = get_irn_irg(block);
4639         if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4640                 return false;
4641         return get_Block_dom_depth(block) < 0;
4642 }
4643
4644 static ir_node *transform_node_Block(ir_node *block)
4645 {
4646         ir_graph *irg   = get_irn_irg(block);
4647         int       arity = get_irn_arity(block);
4648         ir_node  *bad   = NULL;
4649         int       i;
4650
4651         if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4652                 return block;
4653
4654         for (i = 0; i < arity; ++i) {
4655                 ir_node *const pred = get_Block_cfgpred(block, i);
4656                 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4657                         continue;
4658                 if (bad == NULL)
4659                         bad = new_r_Bad(irg, mode_X);
4660                 set_irn_n(block, i, bad);
4661         }
4662
4663         return block;
4664 }
4665
4666 static ir_node *transform_node_Phi(ir_node *phi)
4667 {
4668         int       n     = get_irn_arity(phi);
4669         ir_mode  *mode  = get_irn_mode(phi);
4670         ir_node  *block = get_nodes_block(phi);
4671         ir_graph *irg   = get_irn_irg(phi);
4672         ir_node  *bad   = NULL;
4673         int       i;
4674
4675         /* Set phi-operands for bad-block inputs to bad */
4676         for (i = 0; i < n; ++i) {
4677                 if (!is_Bad(get_Phi_pred(phi, i))) {
4678                         ir_node *pred = get_Block_cfgpred(block, i);
4679                         if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4680                                 if (bad == NULL)
4681                                         bad = new_r_Bad(irg, mode);
4682                                 set_irn_n(phi, i, bad);
4683                         }
4684                 }
4685         }
4686
4687         /* Move Pin nodes down through Phi nodes. */
4688         if (mode == mode_M) {
4689                 n = get_irn_arity(phi);
4690
4691                 /* Beware of Phi0 */
4692                 if (n > 0) {
4693                         ir_node **in;
4694                         ir_node  *new_phi;
4695                         bool      has_pin = false;
4696
4697                         NEW_ARR_A(ir_node *, in, n);
4698
4699                         for (i = 0; i < n; ++i) {
4700                                 ir_node *pred = get_irn_n(phi, i);
4701
4702                                 if (is_Pin(pred)) {
4703                                         in[i]   = get_Pin_op(pred);
4704                                         has_pin = true;
4705                                 } else if (is_Bad(pred)) {
4706                                         in[i] = pred;
4707                                 } else {
4708                                         return phi;
4709                                 }
4710                         }
4711
4712                         if (!has_pin)
4713                                 return phi;
4714
4715                         /* Move the Pin nodes "behind" the Phi. */
4716                         block   = get_irn_n(phi, -1);
4717                         new_phi = new_r_Phi(block, n, in, mode_M);
4718                         return new_r_Pin(block, new_phi);
4719                 }
4720         }
4721         /* Move Confirms down through Phi nodes. */
4722         else if (mode_is_reference(mode)) {
4723                 n = get_irn_arity(phi);
4724
4725                 /* Beware of Phi0 */
4726                 if (n > 0) {
4727                         ir_node    *pred = get_irn_n(phi, 0);
4728                         ir_node    *bound, *new_phi, *block, **in;
4729                         ir_relation relation;
4730                         bool        has_confirm = false;
4731
4732                         if (! is_Confirm(pred))
4733                                 return phi;
4734
4735                         bound    = get_Confirm_bound(pred);
4736                         relation = get_Confirm_relation(pred);
4737
4738                         NEW_ARR_A(ir_node *, in, n);
4739                         in[0] = get_Confirm_value(pred);
4740
4741                         for (i = 1; i < n; ++i) {
4742                                 pred = get_irn_n(phi, i);
4743
4744                                 if (is_Confirm(pred) &&
4745                                                 get_Confirm_bound(pred) == bound &&
4746                                                 get_Confirm_relation(pred) == relation) {
4747                                         in[i]       = get_Confirm_value(pred);
4748                                         has_confirm = true;
4749                                 } else if (is_Bad(pred)) {
4750                                         in[i] = pred;
4751                                 } else {
4752                                         return phi;
4753                                 }
4754                         }
4755
4756                         if (!has_confirm)
4757                                 return phi;
4758
4759                         /* move the Confirm nodes "behind" the Phi */
4760                         block = get_irn_n(phi, -1);
4761                         new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4762                         return new_r_Confirm(block, new_phi, bound, relation);
4763                 }
4764         }
4765         return phi;
4766 }
4767
4768 /**
4769  * Returns the operands of a commutative bin-op, if one operand is
4770  * a const, it is returned as the second one.
4771  */
4772 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4773 {
4774         ir_node *op_a = get_binop_left(binop);
4775         ir_node *op_b = get_binop_right(binop);
4776
4777         assert(is_op_commutative(get_irn_op(binop)));
4778
4779         if (is_Const(op_a)) {
4780                 *a = op_b;
4781                 *c = op_a;
4782         } else {
4783                 *a = op_a;
4784                 *c = op_b;
4785         }
4786 }  /* get_comm_Binop_Ops */
4787
4788 /**
4789  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4790  * Such pattern may arise in bitfield stores.
4791  *
4792  * value  c4                  value      c4 & c2
4793  *    AND     c3                    AND           c1 | c3
4794  *        OR     c2      ===>               OR
4795  *           AND    c1
4796  *               OR
4797  *
4798  *
4799  * value  c2                 value  c1
4800  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4801  *        OR
4802  */
4803 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4804 {
4805         ir_node *irn_and, *c1;
4806         ir_node *or_l, *c2;
4807         ir_node *and_l, *c3;
4808         ir_node *value, *c4;
4809         ir_node *new_and, *new_const, *block;
4810         ir_mode *mode = get_irn_mode(irn_or);
4811
4812         ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4813
4814         for (;;) {
4815                 ir_graph *irg;
4816                 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4817                 if (!is_Const(c1) || !is_And(irn_and))
4818                         return irn_or;
4819
4820                 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4821                 if (!is_Const(c2))
4822                         return irn_or;
4823
4824                 tv1 = get_Const_tarval(c1);
4825                 tv2 = get_Const_tarval(c2);
4826
4827                 tv = tarval_or(tv1, tv2);
4828                 if (tarval_is_all_one(tv)) {
4829                         /* the AND does NOT clear a bit with isn't set by the OR */
4830                         set_Or_left(irn_or, or_l);
4831                         set_Or_right(irn_or, c1);
4832
4833                         /* check for more */
4834                         continue;
4835                 }
4836
4837                 if (!is_Or(or_l))
4838                         return irn_or;
4839
4840                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4841                 if (!is_Const(c3) || !is_And(and_l))
4842                         return irn_or;
4843
4844                 get_comm_Binop_Ops(and_l, &value, &c4);
4845                 if (!is_Const(c4))
4846                         return irn_or;
4847
4848                 /* ok, found the pattern, check for conditions */
4849                 assert(mode == get_irn_mode(irn_and));
4850                 assert(mode == get_irn_mode(or_l));
4851                 assert(mode == get_irn_mode(and_l));
4852
4853                 tv3 = get_Const_tarval(c3);
4854                 tv4 = get_Const_tarval(c4);
4855
4856                 tv = tarval_or(tv4, tv2);
4857                 if (!tarval_is_all_one(tv)) {
4858                         /* have at least one 0 at the same bit position */
4859                         return irn_or;
4860                 }
4861
4862                 if (tv3 != tarval_andnot(tv3, tv4)) {
4863                         /* bit in the or_mask is outside the and_mask */
4864                         return irn_or;
4865                 }
4866
4867                 if (tv1 != tarval_andnot(tv1, tv2)) {
4868                         /* bit in the or_mask is outside the and_mask */
4869                         return irn_or;
4870                 }
4871
4872                 /* ok, all conditions met */
4873                 block = get_irn_n(irn_or, -1);
4874                 irg   = get_irn_irg(block);
4875
4876                 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4877
4878                 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4879
4880                 set_Or_left(irn_or, new_and);
4881                 set_Or_right(irn_or, new_const);
4882
4883                 /* check for more */
4884         }
4885 }  /* transform_node_Or_bf_store */
4886
4887 /**
4888  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4889  */
4890 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4891 {
4892         ir_mode *mode = get_irn_mode(irn_or);
4893         ir_node *shl, *shr, *block;
4894         ir_node *irn, *x, *c1, *c2, *n;
4895         ir_tarval *tv1, *tv2;
4896
4897         /* some backends can't handle rotl */
4898         if (!be_get_backend_param()->support_rotl)
4899                 return irn_or;
4900
4901         if (! mode_is_int(mode))
4902                 return irn_or;
4903
4904         shl = get_binop_left(irn_or);
4905         shr = get_binop_right(irn_or);
4906
4907         if (is_Shr(shl)) {
4908                 if (!is_Shl(shr))
4909                         return irn_or;
4910
4911                 irn = shl;
4912                 shl = shr;
4913                 shr = irn;
4914         } else if (!is_Shl(shl)) {
4915                 return irn_or;
4916         } else if (!is_Shr(shr)) {
4917                 return irn_or;
4918         }
4919         x = get_Shl_left(shl);
4920         if (x != get_Shr_left(shr))
4921                 return irn_or;
4922
4923         c1 = get_Shl_right(shl);
4924         c2 = get_Shr_right(shr);
4925         if (is_Const(c1) && is_Const(c2)) {
4926                 tv1 = get_Const_tarval(c1);
4927                 if (! tarval_is_long(tv1))
4928                         return irn_or;
4929
4930                 tv2 = get_Const_tarval(c2);
4931                 if (! tarval_is_long(tv2))
4932                         return irn_or;
4933
4934                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4935                                 != (int) get_mode_size_bits(mode))
4936                         return irn_or;
4937
4938                 /* yet, condition met */
4939                 block = get_nodes_block(irn_or);
4940
4941                 n = new_r_Rotl(block, x, c1, mode);
4942
4943                 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4944                 return n;
4945         }
4946
4947         /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4948          * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4949         if (!ir_is_negated_value(c1, c2)) {
4950                 return irn_or;
4951         }
4952
4953         /* yet, condition met */
4954         block = get_nodes_block(irn_or);
4955         n = new_r_Rotl(block, x, c1, mode);
4956         DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4957         return n;
4958 }  /* transform_node_Or_Rotl */
4959
4960 static bool is_cmp_unequal(const ir_node *node)
4961 {
4962         ir_relation relation = get_Cmp_relation(node);
4963         ir_node    *left     = get_Cmp_left(node);
4964         ir_node    *right    = get_Cmp_right(node);
4965         ir_mode    *mode     = get_irn_mode(left);
4966
4967         if (relation == ir_relation_less_greater)
4968                 return true;
4969
4970         if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
4971                 return relation == ir_relation_greater;
4972         return false;
4973 }
4974
4975 /**
4976  * returns true for Cmp(x == 0) or Cmp(x != 0)
4977  */
4978 static bool is_cmp_equality_zero(const ir_node *node)
4979 {
4980         ir_relation relation;
4981         ir_node    *right    = get_Cmp_right(node);
4982
4983         if (!is_Const(right) || !is_Const_null(right))
4984                 return false;
4985         relation = get_Cmp_relation(node);
4986         return relation == ir_relation_equal
4987                 || relation == ir_relation_less_greater
4988                 || (!mode_is_signed(get_irn_mode(right))
4989                     && relation == ir_relation_greater);
4990 }
4991
4992 /**
4993  * Transform an Or.
4994  */
4995 static ir_node *transform_node_Or(ir_node *n)
4996 {
4997         ir_node *c, *oldn = n;
4998         ir_node *a = get_Or_left(n);
4999         ir_node *b = get_Or_right(n);
5000         ir_mode *mode;
5001
5002         if (is_Not(a) && is_Not(b)) {
5003                 /* ~a | ~b = ~(a&b) */
5004                 ir_node *block = get_nodes_block(n);
5005
5006                 mode = get_irn_mode(n);
5007                 a = get_Not_op(a);
5008                 b = get_Not_op(b);
5009                 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5010                 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5011                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5012                 return n;
5013         }
5014
5015         /* we can combine the relations of two compares with the same operands */
5016         if (is_Cmp(a) && is_Cmp(b)) {
5017                 ir_node *a_left  = get_Cmp_left(a);
5018                 ir_node *a_right = get_Cmp_right(a);
5019                 ir_node *b_left  = get_Cmp_left(b);
5020                 ir_node *b_right = get_Cmp_right(b);
5021                 if (a_left == b_left && b_left == b_right) {
5022                         dbg_info   *dbgi         = get_irn_dbg_info(n);
5023                         ir_node    *block        = get_nodes_block(n);
5024                         ir_relation a_relation   = get_Cmp_relation(a);
5025                         ir_relation b_relation   = get_Cmp_relation(b);
5026                         ir_relation new_relation = a_relation | b_relation;
5027                         return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
5028                 }
5029                 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
5030                 if (is_cmp_unequal(a) && is_cmp_unequal(b)
5031                     && !mode_is_float(get_irn_mode(a_left))
5032                     && !mode_is_float(get_irn_mode(b_left))) {
5033                         if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
5034                                 ir_graph *irg    = get_irn_irg(n);
5035                                 dbg_info *dbgi   = get_irn_dbg_info(n);
5036                                 ir_node  *block  = get_nodes_block(n);
5037                                 ir_mode  *a_mode = get_irn_mode(a_left);
5038                                 ir_mode  *b_mode = get_irn_mode(b_left);
5039                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5040                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5041                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xora, b_mode);
5042                                 ir_node  *or     = new_rd_Or(dbgi, block, conv, xorb, b_mode);
5043                                 ir_node  *zero   = create_zero_const(irg, b_mode);
5044                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5045                         }
5046                         if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
5047                                 ir_graph *irg    = get_irn_irg(n);
5048                                 dbg_info *dbgi   = get_irn_dbg_info(n);
5049                                 ir_node  *block  = get_nodes_block(n);
5050                                 ir_mode  *a_mode = get_irn_mode(a_left);
5051                                 ir_mode  *b_mode = get_irn_mode(b_left);
5052                                 ir_node  *xora   = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5053                                 ir_node  *xorb   = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5054                                 ir_node  *conv   = new_rd_Conv(dbgi, block, xorb, a_mode);
5055                                 ir_node  *or     = new_rd_Or(dbgi, block, xora, conv, a_mode);
5056                                 ir_node  *zero   = create_zero_const(irg, a_mode);
5057                                 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5058                         }
5059                 }
5060         }
5061
5062         mode = get_irn_mode(n);
5063         HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5064
5065         n = transform_node_Or_bf_store(n);
5066         n = transform_node_Or_Rotl(n);
5067         if (n != oldn)
5068                 return n;
5069
5070         n = transform_bitwise_distributive(n, transform_node_Or);
5071         if (is_Or(n))
5072                 n = transform_node_bitop_shift(n);
5073
5074         return n;
5075 }  /* transform_node_Or */
5076
5077
5078 /* forward */
5079 static ir_node *transform_node(ir_node *n);
5080
5081 /**
5082  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5083  *
5084  * Should be moved to reassociation?
5085  */
5086 static ir_node *transform_node_shift(ir_node *n)
5087 {
5088         ir_node *left, *right;
5089         ir_mode *mode;
5090         ir_mode *count_mode;
5091         ir_tarval *tv1, *tv2, *res;
5092         ir_node *in[2], *irn, *block;
5093         ir_graph *irg;
5094         int       modulo_shf;
5095
5096         left = get_binop_left(n);
5097
5098         /* different operations */
5099         if (get_irn_op(left) != get_irn_op(n))
5100                 return n;
5101
5102         right = get_binop_right(n);
5103         tv1   = value_of(right);
5104         if (tv1 == tarval_bad)
5105                 return n;
5106
5107         tv2 = value_of(get_binop_right(left));
5108         if (tv2 == tarval_bad)
5109                 return n;
5110
5111         count_mode = get_tarval_mode(tv1);
5112         if (get_tarval_mode(tv2) != count_mode) {
5113                 /* TODO: search bigger mode or something and convert... */
5114                 return n;
5115         }
5116
5117         mode       = get_irn_mode(n);
5118         modulo_shf = get_mode_modulo_shift(mode);
5119
5120         if (modulo_shf > 0) {
5121                 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5122
5123                 /* I'm not so sure what happens in one complement... */
5124                 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5125                 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5126                  * above will be invalid) */
5127                 assert(modulo_shf<=0 || is_po2(modulo_shf));
5128
5129                 tv1 = tarval_and(tv1, modulo_mask);
5130                 tv2 = tarval_and(tv2, modulo_mask);
5131         }
5132         res = tarval_add(tv1, tv2);
5133         irg = get_irn_irg(n);
5134
5135         /* beware: a simple replacement works only, if res < modulo shift */
5136         if (is_Rotl(n)) {
5137                 int        bits   = get_mode_size_bits(mode);
5138                 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5139                 res = tarval_mod(res, modulo);
5140         } else {
5141                 long       bits      = get_mode_size_bits(mode);
5142                 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5143
5144                 /* shifting too much */
5145                 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5146                         if (is_Shrs(n)) {
5147                                 ir_node  *block = get_nodes_block(n);
5148                                 dbg_info *dbgi  = get_irn_dbg_info(n);
5149                                 ir_mode  *smode = get_irn_mode(right);
5150                                 ir_node  *cnst  = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5151                                 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5152                         }
5153
5154                         return new_r_Const(irg, get_mode_null(mode));
5155                 }
5156         }
5157
5158         /* ok, we can replace it */
5159         assert(modulo_shf >= (int) get_mode_size_bits(mode));
5160         block = get_nodes_block(n);
5161
5162         in[0] = get_binop_left(left);
5163         in[1] = new_r_Const(irg, res);
5164
5165         irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5166
5167         DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5168
5169         return transform_node(irn);
5170 }
5171
5172 /**
5173  * normalisation:
5174  *    (x << c1) >> c2  <=>  x OP (c2-c1) & ((-1 << c1) >> c2)
5175  *    also:
5176  *    (x >> c1) << c2  <=>  x OP (c2-c1) & ((-1 >> c1) << c2)
5177  *      (also with x >>s c1  when c1>=c2)
5178  */
5179 static ir_node *transform_node_shl_shr(ir_node *n)
5180 {
5181         ir_node   *left;
5182         ir_node   *right = get_binop_right(n);
5183         ir_node   *x;
5184         ir_node   *block;
5185         ir_mode   *mode;
5186         dbg_info  *dbgi;
5187         ir_node   *new_const;
5188         ir_node   *new_shift;
5189         ir_node   *new_and;
5190         ir_tarval *tv_shl;
5191         ir_tarval *tv_shr;
5192         ir_tarval *tv_shift;
5193         ir_tarval *tv_mask;
5194         ir_graph  *irg;
5195         ir_relation relation;
5196         int        need_shrs = 0;
5197
5198         assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5199
5200         if (!is_Const(right))
5201                 return n;
5202
5203         left = get_binop_left(n);
5204         mode = get_irn_mode(n);
5205         if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5206                 ir_node *shr_right = get_binop_right(left);
5207
5208                 if (!is_Const(shr_right))
5209                         return n;
5210
5211                 x      = get_binop_left(left);
5212                 tv_shr = get_Const_tarval(shr_right);
5213                 tv_shl = get_Const_tarval(right);
5214
5215                 if (is_Shrs(left)) {
5216                         /* shrs variant only allowed if c1 >= c2 */
5217                         if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5218                                 return n;
5219
5220                         tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5221                         need_shrs = 1;
5222                 } else {
5223                         tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5224                 }
5225                 tv_mask = tarval_shl(tv_mask, tv_shl);
5226         } else if (is_Shr(n) && is_Shl(left)) {
5227                 ir_node *shl_right = get_Shl_right(left);
5228
5229                 if (!is_Const(shl_right))
5230                         return n;
5231
5232                 x      = get_Shl_left(left);
5233                 tv_shr = get_Const_tarval(right);
5234                 tv_shl = get_Const_tarval(shl_right);
5235
5236                 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5237                 tv_mask = tarval_shr(tv_mask, tv_shr);
5238         } else {
5239                 return n;
5240         }
5241
5242         if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5243                 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5244         }
5245
5246         assert(tv_mask != tarval_bad);
5247         assert(get_tarval_mode(tv_mask) == mode);
5248
5249         block = get_nodes_block(n);
5250         irg   = get_irn_irg(block);
5251         dbgi  = get_irn_dbg_info(n);
5252
5253         relation = tarval_cmp(tv_shl, tv_shr);
5254         if (relation == ir_relation_less || relation == ir_relation_equal) {
5255                 tv_shift  = tarval_sub(tv_shr, tv_shl, NULL);
5256                 new_const = new_r_Const(irg, tv_shift);
5257                 if (need_shrs) {
5258                         new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5259                 } else {
5260                         new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5261                 }
5262         } else {
5263                 assert(relation == ir_relation_greater);
5264                 tv_shift  = tarval_sub(tv_shl, tv_shr, NULL);
5265                 new_const = new_r_Const(irg, tv_shift);
5266                 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5267         }
5268
5269         new_const = new_r_Const(irg, tv_mask);
5270         new_and   = new_rd_And(dbgi, block, new_shift, new_const, mode);
5271
5272         return new_and;
5273 }
5274
5275 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5276 {
5277         ir_mode   *mode      = get_tarval_mode(tv);
5278         ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5279         return tarval_mod(tv, modulo_tv);
5280 }
5281
5282 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5283                                   ir_node *left, ir_node *right, ir_mode *mode);
5284
5285 /**
5286  * Normalisation: if we have a shl/shr with modulo_shift behaviour
5287  * then we can use that to minimize the value of Add(x, const) or
5288  * Sub(Const, x). In particular this often avoids 1 instruction in some
5289  * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5290  * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5291  */
5292 static ir_node *transform_node_shift_modulo(ir_node *n,
5293                                             new_shift_func new_shift)
5294 {
5295         ir_mode  *mode   = get_irn_mode(n);
5296         int       modulo = get_mode_modulo_shift(mode);
5297         ir_node  *newop  = NULL;
5298         ir_mode  *mode_right;
5299         ir_node  *block;
5300         ir_node  *right;
5301         ir_graph *irg;
5302
5303         if (modulo == 0)
5304                 return n;
5305         if (get_mode_arithmetic(mode) != irma_twos_complement)
5306                 return n;
5307         if (!is_po2(modulo))
5308                 return n;
5309
5310         irg        = get_irn_irg(n);
5311         block      = get_nodes_block(n);
5312         right      = get_binop_right(n);
5313         mode_right = get_irn_mode(right);
5314         if (is_Const(right)) {
5315                 ir_tarval *tv     = get_Const_tarval(right);
5316                 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5317
5318                 if (tv_mod == tv)
5319                         return n;
5320
5321                 newop = new_r_Const(irg, tv_mod);
5322         } else if (is_Add(right)) {
5323                 ir_node *add_right = get_Add_right(right);
5324                 if (is_Const(add_right)) {
5325                         ir_tarval *tv     = get_Const_tarval(add_right);
5326                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5327                         ir_node   *newconst;
5328                         if (tv_mod == tv)
5329                                 return n;
5330
5331                         newconst = new_r_Const(irg, tv_mod);
5332                         newop    = new_r_Add(block, get_Add_left(right), newconst,
5333                                              mode_right);
5334                 }
5335         } else if (is_Sub(right)) {
5336                 ir_node *sub_left = get_Sub_left(right);
5337                 if (is_Const(sub_left)) {
5338                         ir_tarval *tv     = get_Const_tarval(sub_left);
5339                         ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5340                         ir_node  *newconst;
5341                         if (tv_mod == tv)
5342                                 return n;
5343
5344                         newconst = new_r_Const(irg, tv_mod);
5345                         newop    = new_r_Sub(block, newconst, get_Sub_right(right),
5346                                              mode_right);
5347                 }
5348         } else {
5349                 return n;
5350         }
5351
5352         if (newop != NULL) {
5353                 dbg_info *dbgi = get_irn_dbg_info(n);
5354                 ir_node  *left = get_binop_left(n);
5355                 return new_shift(dbgi, block, left, newop, mode);
5356         }
5357         return n;
5358 }
5359
5360 /**
5361  * Transform a Shr.
5362  */
5363 static ir_node *transform_node_Shr(ir_node *n)
5364 {
5365         ir_node *c, *oldn = n;
5366         ir_node *left  = get_Shr_left(n);
5367         ir_node *right = get_Shr_right(n);
5368         ir_mode *mode  = get_irn_mode(n);
5369
5370         HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5371         n = transform_node_shift(n);
5372
5373         if (is_Shr(n))
5374                 n = transform_node_shift_modulo(n, new_rd_Shr);
5375         if (is_Shr(n))
5376                 n = transform_node_shl_shr(n);
5377         if (is_Shr(n))
5378                 n = transform_node_shift_bitop(n);
5379
5380         return n;
5381 }  /* transform_node_Shr */
5382
5383 /**
5384  * Transform a Shrs.
5385  */
5386 static ir_node *transform_node_Shrs(ir_node *n)
5387 {
5388         ir_node *c, *oldn = n;
5389         ir_node *a    = get_Shrs_left(n);
5390         ir_node *b    = get_Shrs_right(n);
5391         ir_mode *mode = get_irn_mode(n);
5392
5393         if (is_oversize_shift(n)) {
5394                 ir_node  *block = get_nodes_block(n);
5395                 dbg_info *dbgi  = get_irn_dbg_info(n);
5396                 ir_mode  *cmode = get_irn_mode(b);
5397                 long      val   = get_mode_size_bits(cmode)-1;
5398                 ir_graph *irg   = get_irn_irg(n);
5399                 ir_node  *cnst  = new_r_Const_long(irg, cmode, val);
5400                 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5401         }
5402
5403         HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5404         n = transform_node_shift(n);
5405
5406         if (is_Shrs(n))
5407                 n = transform_node_shift_modulo(n, new_rd_Shrs);
5408         if (is_Shrs(n))
5409                 n = transform_node_shift_bitop(n);
5410
5411         return n;
5412 }  /* transform_node_Shrs */
5413
5414 /**
5415  * Transform a Shl.
5416  */
5417 static ir_node *transform_node_Shl(ir_node *n)
5418 {
5419         ir_node *c, *oldn = n;
5420         ir_node *a    = get_Shl_left(n);
5421         ir_node *b    = get_Shl_right(n);
5422         ir_mode *mode = get_irn_mode(n);
5423
5424         HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5425         n = transform_node_shift(n);
5426
5427         if (is_Shl(n))
5428                 n = transform_node_shift_modulo(n, new_rd_Shl);
5429         if (is_Shl(n))
5430                 n = transform_node_shl_shr(n);
5431         if (is_Shl(n))
5432                 n = transform_node_shift_bitop(n);
5433
5434         return n;
5435 }  /* transform_node_Shl */
5436
5437 /**
5438  * Transform a Rotl.
5439  */
5440 static ir_node *transform_node_Rotl(ir_node *n)
5441 {
5442         ir_node *c, *oldn = n;
5443         ir_node *a    = get_Rotl_left(n);
5444         ir_node *b    = get_Rotl_right(n);
5445         ir_mode *mode = get_irn_mode(n);
5446
5447         HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5448         n = transform_node_shift(n);
5449
5450         if (is_Rotl(n))
5451                 n = transform_node_shift_bitop(n);
5452
5453         return n;
5454 }  /* transform_node_Rotl */
5455
5456 /**
5457  * Transform a Conv.
5458  */
5459 static ir_node *transform_node_Conv(ir_node *n)
5460 {
5461         ir_node *c, *oldn = n;
5462         ir_mode *mode = get_irn_mode(n);
5463         ir_node *a    = get_Conv_op(n);
5464
5465         if (mode != mode_b && is_const_Phi(a)) {
5466                 /* Do NOT optimize mode_b Conv's, this leads to remaining
5467                  * Phib nodes later, because the conv_b_lower operation
5468                  * is instantly reverted, when it tries to insert a Convb.
5469                  */
5470                 c = apply_conv_on_phi(a, mode);
5471                 if (c) {
5472                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5473                         return c;
5474                 }
5475         }
5476
5477         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5478                 ir_graph *irg = get_irn_irg(n);
5479                 return new_r_Unknown(irg, mode);
5480         }
5481
5482         if (mode_is_reference(mode) &&
5483                 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5484                 is_Add(a)) {
5485                 ir_node *l = get_Add_left(a);
5486                 ir_node *r = get_Add_right(a);
5487                 dbg_info *dbgi = get_irn_dbg_info(a);
5488                 ir_node *block = get_nodes_block(n);
5489                 if (is_Conv(l)) {
5490                         ir_node *lop = get_Conv_op(l);
5491                         if (get_irn_mode(lop) == mode) {
5492                                 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5493                                 n = new_rd_Add(dbgi, block, lop, r, mode);
5494                                 return n;
5495                         }
5496                 }
5497                 if (is_Conv(r)) {
5498                         ir_node *rop = get_Conv_op(r);
5499                         if (get_irn_mode(rop) == mode) {
5500                                 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5501                                 n = new_rd_Add(dbgi, block, l, rop, mode);
5502                                 return n;
5503                         }
5504                 }
5505         }
5506
5507         return n;
5508 }  /* transform_node_Conv */
5509
5510 /**
5511  * Remove dead blocks and nodes in dead blocks
5512  * in keep alive list.  We do not generate a new End node.
5513  */
5514 static ir_node *transform_node_End(ir_node *n)
5515 {
5516         int i, j, n_keepalives = get_End_n_keepalives(n);
5517         ir_node **in;
5518
5519         NEW_ARR_A(ir_node *, in, n_keepalives);
5520
5521         for (i = j = 0; i < n_keepalives; ++i) {
5522                 ir_node *ka = get_End_keepalive(n, i);
5523                 ir_node *block;
5524                 /* no need to keep Bad */
5525                 if (is_Bad(ka))
5526                         continue;
5527                 /* do not keep unreachable code */
5528                 block = is_Block(ka) ? ka : get_nodes_block(ka);
5529                 if (is_block_unreachable(block))
5530                         continue;
5531                 in[j++] = ka;
5532         }
5533         if (j != n_keepalives)
5534                 set_End_keepalives(n, j, in);
5535         return n;
5536 }  /* transform_node_End */
5537
5538 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5539 {
5540         if (is_Minus(a) && get_Minus_op(a) == b)
5541                 return true;
5542         if (is_Minus(b) && get_Minus_op(b) == a)
5543                 return true;
5544         if (is_Sub(a) && is_Sub(b)) {
5545                 ir_node *a_left  = get_Sub_left(a);
5546                 ir_node *a_right = get_Sub_right(a);
5547                 ir_node *b_left  = get_Sub_left(b);
5548                 ir_node *b_right = get_Sub_right(b);
5549
5550                 if (a_left == b_right && a_right == b_left)
5551                         return true;
5552         }
5553
5554         return false;
5555 }
5556
5557 static const ir_node *skip_upconv(const ir_node *node)
5558 {
5559         while (is_Conv(node)) {
5560                 ir_mode       *mode    = get_irn_mode(node);
5561                 const ir_node *op      = get_Conv_op(node);
5562                 ir_mode       *op_mode = get_irn_mode(op);
5563                 if (!smaller_mode(op_mode, mode))
5564                         break;
5565                 node = op;
5566         }
5567         return node;
5568 }
5569
5570 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_true,
5571                   const ir_node *mux_false)
5572 {
5573         ir_node    *cmp_left;
5574         ir_node    *cmp_right;
5575         ir_mode    *mode;
5576         ir_relation relation;
5577
5578         if (!is_Cmp(sel))
5579                 return 0;
5580
5581         /**
5582          * Note further that these optimization work even for floating point
5583          * with NaN's because -NaN == NaN.
5584          * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5585          * transformations.
5586          */
5587         mode = get_irn_mode(mux_true);
5588         if (mode_honor_signed_zeros(mode))
5589                 return 0;
5590
5591         /* must be <, <=, >=, > */
5592         relation = get_Cmp_relation(sel);
5593         if ((relation & ir_relation_less_greater) == 0)
5594                 return 0;
5595
5596         if (!ir_is_negated_value(mux_true, mux_false))
5597                 return 0;
5598
5599         mux_true  = skip_upconv(mux_true);
5600         mux_false = skip_upconv(mux_false);
5601
5602         /* must be x cmp 0 */
5603         cmp_right = get_Cmp_right(sel);
5604         if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5605                 return 0;
5606
5607         cmp_left = get_Cmp_left(sel);
5608         if (cmp_left == mux_false) {
5609                 if (relation & ir_relation_less) {
5610                         return 1;
5611                 } else {
5612                         assert(relation & ir_relation_greater);
5613                         return -1;
5614                 }
5615         } else if (cmp_left == mux_true) {
5616                 if (relation & ir_relation_less) {
5617                         return -1;
5618                 } else {
5619                         assert(relation & ir_relation_greater);
5620                         return 1;
5621                 }
5622         }
5623
5624         return 0;
5625 }
5626
5627 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_true,
5628                        ir_node *mux_false)
5629 {
5630         ir_node *cmp_left = get_Cmp_left(sel);
5631         return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5632 }
5633
5634 /**
5635  * Optimize a Mux into some simpler cases.
5636  */
5637 static ir_node *transform_node_Mux(ir_node *n)
5638 {
5639         ir_node  *oldn = n;
5640         ir_node  *sel  = get_Mux_sel(n);
5641         ir_mode  *mode = get_irn_mode(n);
5642         ir_node  *t    = get_Mux_true(n);
5643         ir_node  *f    = get_Mux_false(n);
5644         ir_graph *irg  = get_irn_irg(n);
5645
5646         /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5647         if (get_mode_arithmetic(mode) == irma_twos_complement) {
5648                 int abs = ir_mux_is_abs(sel, t, f);
5649                 if (abs != 0) {
5650                         dbg_info *dbgi       = get_irn_dbg_info(n);
5651                         ir_node  *block      = get_nodes_block(n);
5652                         ir_node  *op         = ir_get_abs_op(sel, t, f);
5653                         int       bits       = get_mode_size_bits(mode);
5654                         ir_node  *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5655                         ir_node  *sext       = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5656                         ir_node  *xorn       = new_rd_Eor(dbgi, block, op, sext, mode);
5657                         ir_node  *res;
5658                         if (abs > 0) {
5659                                 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5660                         } else {
5661                                 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5662                         }
5663                         return res;
5664                 }
5665         }
5666
5667         /* first normalization step: try to move a constant to the false side,
5668          * 0 preferred on false side too */
5669         if (is_Cmp(sel) && is_Const(t) &&
5670                         (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5671                 dbg_info *seldbgi = get_irn_dbg_info(sel);
5672                 ir_node  *block   = get_nodes_block(sel);
5673                 ir_relation relation = get_Cmp_relation(sel);
5674                 ir_node *tmp = t;
5675                 t = f;
5676                 f = tmp;
5677
5678                 /* Mux(x, a, b) => Mux(not(x), b, a) */
5679                 relation = get_negated_relation(relation);
5680                 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5681                                 get_Cmp_right(sel), relation);
5682                 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5683         }
5684
5685         /* the following optimisations create new mode_b nodes, so only do them
5686          * before mode_b lowering */
5687         if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
5688                 if (is_Mux(t)) {
5689                         ir_node*  block = get_nodes_block(n);
5690                         ir_node*  c0    = sel;
5691                         ir_node*  c1    = get_Mux_sel(t);
5692                         ir_node*  t1    = get_Mux_true(t);
5693                         ir_node*  f1    = get_Mux_false(t);
5694                         if (f == f1) {
5695                                 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5696                                 ir_node* and_    = new_r_And(block, c0, c1, mode_b);
5697                                 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5698                                 n   = new_mux;
5699                                 sel = and_;
5700                                 f   = f1;
5701                                 t   = t1;
5702                                 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5703                         } else if (f == t1) {
5704                                 /* Mux(cond0, Mux(cond1, x, y), x) */
5705                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
5706                                 ir_node* and_    = new_r_And(block, c0, not_c1, mode_b);
5707                                 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5708                                 n   = new_mux;
5709                                 sel = and_;
5710                                 f   = t1;
5711                                 t   = f1;
5712                                 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5713                         }
5714                 } else if (is_Mux(f)) {
5715                         ir_node*  block = get_nodes_block(n);
5716                         ir_node*  c0    = sel;
5717                         ir_node*  c1    = get_Mux_sel(f);
5718                         ir_node*  t1    = get_Mux_true(f);
5719                         ir_node*  f1    = get_Mux_false(f);
5720                         if (t == t1) {
5721                                 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5722                                 ir_node* or_     = new_r_Or(block, c0, c1, mode_b);
5723                                 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5724                                 n   = new_mux;
5725                                 sel = or_;
5726                                 f   = f1;
5727                                 t   = t1;
5728                                 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5729                         } else if (t == f1) {
5730                                 /* Mux(cond0, x, Mux(cond1, y, x)) */
5731                                 ir_node* not_c1  = new_r_Not(block, c1, mode_b);
5732                                 ir_node* or_     = new_r_Or(block, c0, not_c1, mode_b);
5733                                 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5734                                 n   = new_mux;
5735                                 sel = or_;
5736                                 f   = t1;
5737                                 t   = f1;
5738                                 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5739                         }
5740                 }
5741
5742                 /* note: after normalization, false can only happen on default */
5743                 if (mode == mode_b) {
5744                         dbg_info *dbg   = get_irn_dbg_info(n);
5745                         ir_node  *block = get_nodes_block(n);
5746
5747                         if (is_Const(t)) {
5748                                 ir_tarval *tv_t = get_Const_tarval(t);
5749                                 if (tv_t == tarval_b_true) {
5750                                         if (is_Const(f)) {
5751                                                 /* Muxb(sel, true, false) = sel */
5752                                                 assert(get_Const_tarval(f) == tarval_b_false);
5753                                                 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5754                                                 return sel;
5755                                         } else {
5756                                                 /* Muxb(sel, true, x) = Or(sel, x) */
5757                                                 n = new_rd_Or(dbg, block, sel, f, mode_b);
5758                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5759                                                 return n;
5760                                         }
5761                                 }
5762                         } else if (is_Const(f)) {
5763                                 ir_tarval *tv_f = get_Const_tarval(f);
5764                                 if (tv_f == tarval_b_true) {
5765                                         /* Muxb(sel, x, true) = Or(Not(sel), x) */
5766                                         ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5767                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5768                                         n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5769                                         return n;
5770                                 } else {
5771                                         /* Muxb(sel, x, false) = And(sel, x) */
5772                                         assert(tv_f == tarval_b_false);
5773                                         n = new_rd_And(dbg, block, sel, t, mode_b);
5774                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5775                                         return n;
5776                                 }
5777                         }
5778                 }
5779
5780                 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5781                  * value to integer. */
5782                 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5783                         ir_tarval *a = get_Const_tarval(t);
5784                         ir_tarval *b = get_Const_tarval(f);
5785
5786                         if (tarval_is_one(a) && tarval_is_null(b)) {
5787                                 ir_node *block = get_nodes_block(n);
5788                                 ir_node *conv  = new_r_Conv(block, sel, mode);
5789                                 n = conv;
5790                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5791                                 return n;
5792                         } else if (tarval_is_null(a) && tarval_is_one(b)) {
5793                                 ir_node *block = get_nodes_block(n);
5794                                 ir_node *not_  = new_r_Not(block, sel, mode_b);
5795                                 ir_node *conv  = new_r_Conv(block, not_, mode);
5796                                 n = conv;
5797                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5798                                 return n;
5799                         }
5800                 }
5801         }
5802
5803         if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5804                 ir_relation relation = get_Cmp_relation(sel);
5805                 ir_node    *cmp_r    = get_Cmp_right(sel);
5806                 ir_node    *cmp_l    = get_Cmp_left(sel);
5807                 ir_node    *block    = get_nodes_block(n);
5808
5809                 if (is_And(cmp_l) && f == cmp_r) {
5810                         ir_node *and_r = get_And_right(cmp_l);
5811                         ir_node *and_l;
5812
5813                         if (and_r == t && is_single_bit(and_r)) {
5814                                 if (relation == ir_relation_equal) {
5815                                         /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5816                                         n = new_rd_Eor(get_irn_dbg_info(n),
5817                                                 block, cmp_l, t, mode);
5818                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5819                                 } else {
5820                                         /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5821                                         n = cmp_l;
5822                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5823                                 }
5824                                 return n;
5825                         }
5826                         and_l = get_And_left(cmp_l);
5827                         if (and_l == t && is_single_bit(and_l)) {
5828                                 if (relation == ir_relation_equal) {
5829                                         /* ((1 << n) & a) == 0, (1 << n), 0) */
5830                                         n = new_rd_Eor(get_irn_dbg_info(n),
5831                                                 block, cmp_l, t, mode);
5832                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5833                                 } else {
5834                                         /* ((1 << n) & a) != 0, (1 << n), 0) */
5835                                         n = cmp_l;
5836                                         DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5837                                 }
5838                                 return n;
5839                         }
5840                 }
5841         }
5842
5843         return n;
5844 }
5845
5846 /**
5847  * optimize Sync nodes that have other syncs as input we simply add the inputs
5848  * of the other sync to our own inputs
5849  */
5850 static ir_node *transform_node_Sync(ir_node *n)
5851 {
5852         int arity = get_Sync_n_preds(n);
5853         int i;
5854
5855         for (i = 0; i < arity;) {
5856                 ir_node *pred = get_Sync_pred(n, i);
5857                 int      pred_arity;
5858                 int      j;
5859
5860                 /* Remove Bad predecessors */
5861                 if (is_Bad(pred)) {
5862                         del_Sync_n(n, i);
5863                         --arity;
5864                         continue;
5865                 }
5866
5867                 /* Remove duplicate predecessors */
5868                 for (j = 0; j < i; ++j) {
5869                         if (get_Sync_pred(n, j) == pred) {
5870                                 del_Sync_n(n, i);
5871                                 --arity;
5872                                 break;
5873                         }
5874                 }
5875                 if (j < i)
5876                         continue;
5877
5878                 if (!is_Sync(pred)) {
5879                         ++i;
5880                         continue;
5881                 }
5882
5883                 del_Sync_n(n, i);
5884                 --arity;
5885
5886                 pred_arity = get_Sync_n_preds(pred);
5887                 for (j = 0; j < pred_arity; ++j) {
5888                         ir_node *pred_pred = get_Sync_pred(pred, j);
5889                         int      k;
5890
5891                         for (k = 0;; ++k) {
5892                                 if (k >= arity) {
5893                                         add_irn_n(n, pred_pred);
5894                                         ++arity;
5895                                         break;
5896                                 }
5897                                 if (get_Sync_pred(n, k) == pred_pred) break;
5898                         }
5899                 }
5900         }
5901
5902         if (arity == 0) {
5903                 ir_graph *irg = get_irn_irg(n);
5904                 return new_r_Bad(irg, mode_M);
5905         }
5906         if (arity == 1) {
5907                 return get_Sync_pred(n, 0);
5908         }
5909
5910         /* rehash the sync node */
5911         add_identities(n);
5912         return n;
5913 }
5914
5915 static ir_node *transform_node_Load(ir_node *n)
5916 {
5917         /* if our memory predecessor is a load from the same address, then reuse the
5918          * previous result */
5919         ir_node *mem = get_Load_mem(n);
5920         ir_node *mem_pred;
5921
5922         if (!is_Proj(mem))
5923                 return n;
5924         /* don't touch volatile loads */
5925         if (get_Load_volatility(n) == volatility_is_volatile)
5926                 return n;
5927         mem_pred = get_Proj_pred(mem);
5928         if (is_Load(mem_pred)) {
5929                 ir_node *pred_load = mem_pred;
5930
5931                 /* conservatively compare the 2 loads. TODO: This could be less strict
5932                  * with fixup code in some situations (like smaller/bigger modes) */
5933                 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5934                         return n;
5935                 if (get_Load_mode(pred_load) != get_Load_mode(n))
5936                         return n;
5937                 /* all combinations of aligned/unaligned pred/n should be fine so we do
5938                  * not compare the unaligned attribute */
5939                 {
5940                         ir_node  *block = get_nodes_block(n);
5941                         ir_node  *jmp   = new_r_Jmp(block);
5942                         ir_graph *irg   = get_irn_irg(n);
5943                         ir_node  *bad   = new_r_Bad(irg, mode_X);
5944                         ir_mode  *mode  = get_Load_mode(n);
5945                         ir_node  *res   = new_r_Proj(pred_load, mode, pn_Load_res);
5946                         ir_node  *in[]  = { mem, res, jmp, bad };
5947                         ir_node  *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5948                         return tuple;
5949                 }
5950         } else if (is_Store(mem_pred)) {
5951                 ir_node *pred_store = mem_pred;
5952                 ir_node *value      = get_Store_value(pred_store);
5953
5954                 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5955                         return n;
5956                 if (get_irn_mode(value) != get_Load_mode(n))
5957                         return n;
5958                 /* all combinations of aligned/unaligned pred/n should be fine so we do
5959                  * not compare the unaligned attribute */
5960                 {
5961                         ir_node  *block = get_nodes_block(n);
5962                         ir_node  *jmp   = new_r_Jmp(block);
5963                         ir_graph *irg   = get_irn_irg(n);
5964                         ir_node  *bad   = new_r_Bad(irg, mode_X);
5965                         ir_node  *res   = value;
5966                         ir_node  *in[]  = { mem, res, jmp, bad };
5967                         ir_node  *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5968                         return tuple;
5969                 }
5970         }
5971
5972         return n;
5973 }
5974
5975 /**
5976  * optimize a trampoline Call into a direct Call
5977  */
5978 static ir_node *transform_node_Call(ir_node *call)
5979 {
5980         ir_node  *callee = get_Call_ptr(call);
5981         ir_node  *adr, *mem, *res, *bl, **in;
5982         ir_type  *ctp, *mtp, *tp;
5983         ir_graph *irg;
5984         type_dbg_info *tdb;
5985         dbg_info *db;
5986         size_t   i, n_res, n_param;
5987         ir_variadicity var;
5988
5989         if (! is_Proj(callee))
5990                 return call;
5991         callee = get_Proj_pred(callee);
5992         if (! is_Builtin(callee))
5993                 return call;
5994         if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5995                 return call;
5996
5997         mem = get_Call_mem(call);
5998
5999         if (skip_Proj(mem) == callee) {
6000                 /* memory is routed to the trampoline, skip */
6001                 mem = get_Builtin_mem(callee);
6002         }
6003
6004         /* build a new call type */
6005         mtp = get_Call_type(call);
6006         tdb = get_type_dbg_info(mtp);
6007
6008         n_res   = get_method_n_ress(mtp);
6009         n_param = get_method_n_params(mtp);
6010         ctp     = new_d_type_method(n_param + 1, n_res, tdb);
6011
6012         for (i = 0; i < n_res; ++i)
6013                 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6014
6015         NEW_ARR_A(ir_node *, in, n_param + 1);
6016
6017         /* FIXME: we don't need a new pointer type in every step */
6018         irg = get_irn_irg(call);
6019         tp = get_irg_frame_type(irg);
6020         tp = new_type_pointer(tp);
6021         set_method_param_type(ctp, 0, tp);
6022
6023         in[0] = get_Builtin_param(callee, 2);
6024         for (i = 0; i < n_param; ++i) {
6025                 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6026                 in[i + 1] = get_Call_param(call, i);
6027         }
6028         var = get_method_variadicity(mtp);
6029         set_method_variadicity(ctp, var);
6030         /* When we resolve a trampoline, the function must be called by a this-call */
6031         set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6032         set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6033
6034         adr = get_Builtin_param(callee, 1);
6035
6036         db  = get_irn_dbg_info(call);
6037         bl  = get_nodes_block(call);
6038
6039         res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6040         if (get_irn_pinned(call) == op_pin_state_floats)
6041                 set_irn_pinned(res, op_pin_state_floats);
6042         return res;
6043 }  /* transform_node_Call */
6044
6045 /**
6046  * Tries several [inplace] [optimizing] transformations and returns an
6047  * equivalent node.  The difference to equivalent_node() is that these
6048  * transformations _do_ generate new nodes, and thus the old node must
6049  * not be freed even if the equivalent node isn't the old one.
6050  */
6051 static ir_node *transform_node(ir_node *n)
6052 {
6053         ir_node *oldn;
6054
6055         /*
6056          * Transform_node is the only "optimizing transformation" that might
6057          * return a node with a different opcode. We iterate HERE until fixpoint
6058          * to get the final result.
6059          */
6060         do {
6061                 oldn = n;
6062                 if (n->op->ops.transform_node != NULL)
6063                         n = n->op->ops.transform_node(n);
6064         } while (oldn != n);
6065
6066         return n;
6067 }  /* transform_node */
6068
6069 /**
6070  * Sets the default transform node operation for an ir_op_ops.
6071  *
6072  * @param code   the opcode for the default operation
6073  * @param ops    the operations initialized
6074  *
6075  * @return
6076  *    The operations.
6077  */
6078 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6079 {
6080 #define CASE(a)                                         \
6081         case iro_##a:                                       \
6082                 ops->transform_node      = transform_node_##a;  \
6083                 break
6084 #define CASE_PROJ(a)                                         \
6085         case iro_##a:                                            \
6086                 ops->transform_node_Proj = transform_node_Proj_##a;  \
6087                 break
6088 #define CASE_PROJ_EX(a)                                      \
6089         case iro_##a:                                            \
6090                 ops->transform_node      = transform_node_##a;       \
6091                 ops->transform_node_Proj = transform_node_Proj_##a;  \
6092                 break
6093
6094         switch (code) {
6095         CASE(Add);
6096         CASE(And);
6097         CASE(Block);
6098         CASE(Call);
6099         CASE(Cmp);
6100         CASE(Conv);
6101         CASE(End);
6102         CASE(Eor);
6103         CASE(Minus);
6104         CASE(Mul);
6105         CASE(Mux);
6106         CASE(Not);
6107         CASE(Or);
6108         CASE(Phi);
6109         CASE(Proj);
6110         CASE(Rotl);
6111         CASE(Sel);
6112         CASE(Shl);
6113         CASE(Shr);
6114         CASE(Shrs);
6115         CASE(Sub);
6116         CASE(Sync);
6117         CASE_PROJ(Bound);
6118         CASE_PROJ(CopyB);
6119         CASE_PROJ(Store);
6120         CASE_PROJ_EX(Cond);
6121         CASE_PROJ_EX(Div);
6122         CASE_PROJ_EX(Load);
6123         CASE_PROJ_EX(Mod);
6124         default:
6125                 break;
6126         }
6127
6128         return ops;
6129 #undef CASE_PROJ_EX
6130 #undef CASE_PROJ
6131 #undef CASE
6132 }  /* firm_set_default_transform_node */
6133
6134
6135 /* **************** Common Subexpression Elimination **************** */
6136
6137 /** The size of the hash table used, should estimate the number of nodes
6138     in a graph. */
6139 #define N_IR_NODES 512
6140
6141 /** Compares two exception attributes */
6142 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6143 {
6144         const except_attr *ea = &a->attr.except;
6145         const except_attr *eb = &b->attr.except;
6146         return ea->pin_state != eb->pin_state;
6147 }
6148
6149 /** Compares the attributes of two Const nodes. */
6150 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6151 {
6152         return get_Const_tarval(a) != get_Const_tarval(b);
6153 }
6154
6155 /** Compares the attributes of two Proj nodes. */
6156 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6157 {
6158         return a->attr.proj.proj != b->attr.proj.proj;
6159 }
6160
6161 /** Compares the attributes of two Alloc nodes. */
6162 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6163 {
6164         const alloc_attr *pa = &a->attr.alloc;
6165         const alloc_attr *pb = &b->attr.alloc;
6166         if (pa->where != pb->where || pa->type != pb->type)
6167                 return 1;
6168         return node_cmp_exception(a, b);
6169 }
6170
6171 /** Compares the attributes of two Free nodes. */
6172 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6173 {
6174         const free_attr *pa = &a->attr.free;
6175         const free_attr *pb = &b->attr.free;
6176         return (pa->where != pb->where) || (pa->type != pb->type);
6177 }
6178
6179 /** Compares the attributes of two SymConst nodes. */
6180 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6181 {
6182         const symconst_attr *pa = &a->attr.symc;
6183         const symconst_attr *pb = &b->attr.symc;
6184         return (pa->kind       != pb->kind)
6185             || (pa->sym.type_p != pb->sym.type_p);
6186 }
6187
6188 /** Compares the attributes of two Call nodes. */
6189 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6190 {
6191         const call_attr *pa = &a->attr.call;
6192         const call_attr *pb = &b->attr.call;
6193         if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6194                 return 1;
6195         return node_cmp_exception(a, b);
6196 }
6197
6198 /** Compares the attributes of two Sel nodes. */
6199 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6200 {
6201         const ir_entity *a_ent = get_Sel_entity(a);
6202         const ir_entity *b_ent = get_Sel_entity(b);
6203         return a_ent != b_ent;
6204 }
6205
6206 /** Compares the attributes of two Phi nodes. */
6207 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6208 {
6209         /* we can only enter this function if both nodes have the same number of inputs,
6210            hence it is enough to check if one of them is a Phi0 */
6211         if (is_Phi0(a)) {
6212                 /* check the Phi0 pos attribute */
6213                 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6214         }
6215         return 0;
6216 }
6217
6218 /** Compares the attributes of two Conv nodes. */
6219 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6220 {
6221         return get_Conv_strict(a) != get_Conv_strict(b);
6222 }
6223
6224 /** Compares the attributes of two Cast nodes. */
6225 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6226 {
6227         return get_Cast_type(a) != get_Cast_type(b);
6228 }
6229
6230 /** Compares the attributes of two Load nodes. */
6231 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6232 {
6233         if (get_Load_volatility(a) == volatility_is_volatile ||
6234             get_Load_volatility(b) == volatility_is_volatile)
6235                 /* NEVER do CSE on volatile Loads */
6236                 return 1;
6237         /* do not CSE Loads with different alignment. Be conservative. */
6238         if (get_Load_unaligned(a) != get_Load_unaligned(b))
6239                 return 1;
6240         if (get_Load_mode(a) != get_Load_mode(b))
6241                 return 1;
6242         return node_cmp_exception(a, b);
6243 }
6244
6245 /** Compares the attributes of two Store nodes. */
6246 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6247 {
6248         /* do not CSE Stores with different alignment. Be conservative. */
6249         if (get_Store_unaligned(a) != get_Store_unaligned(b))
6250                 return 1;
6251         /* NEVER do CSE on volatile Stores */
6252         if (get_Store_volatility(a) == volatility_is_volatile ||
6253             get_Store_volatility(b) == volatility_is_volatile)
6254                 return 1;
6255         return node_cmp_exception(a, b);
6256 }
6257
6258 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6259 {
6260         if (get_CopyB_type(a) != get_CopyB_type(b))
6261                 return 1;
6262
6263         return node_cmp_exception(a, b);
6264 }
6265
6266 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6267 {
6268         return node_cmp_exception(a, b);
6269 }
6270
6271 /** Compares the attributes of two Div nodes. */
6272 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6273 {
6274         const div_attr *ma = &a->attr.div;
6275         const div_attr *mb = &b->attr.div;
6276         if (ma->resmode != mb->resmode || ma->no_remainder  != mb->no_remainder)
6277                 return 1;
6278         return node_cmp_exception(a, b);
6279 }
6280
6281 /** Compares the attributes of two Mod nodes. */
6282 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6283 {
6284         const mod_attr *ma = &a->attr.mod;
6285         const mod_attr *mb = &b->attr.mod;
6286         if (ma->resmode != mb->resmode)
6287                 return 1;
6288         return node_cmp_exception(a, b);
6289 }
6290
6291 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6292 {
6293         const cmp_attr *ma = &a->attr.cmp;
6294         const cmp_attr *mb = &b->attr.cmp;
6295         return ma->relation != mb->relation;
6296 }
6297
6298 /** Compares the attributes of two Confirm nodes. */
6299 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6300 {
6301         const confirm_attr *ma = &a->attr.confirm;
6302         const confirm_attr *mb = &b->attr.confirm;
6303         return ma->relation != mb->relation;
6304 }
6305
6306 /** Compares the attributes of two Builtin nodes. */
6307 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6308 {
6309         if (get_Builtin_kind(a) != get_Builtin_kind(b))
6310                 return 1;
6311         if (get_Builtin_type(a) != get_Builtin_type(b))
6312                 return 1;
6313         return node_cmp_exception(a, b);
6314 }
6315
6316 /** Compares the attributes of two ASM nodes. */
6317 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6318 {
6319         int i, n;
6320         const ir_asm_constraint *ca;
6321         const ir_asm_constraint *cb;
6322         ident **cla, **clb;
6323
6324         if (get_ASM_text(a) != get_ASM_text(b))
6325                 return 1;
6326
6327         /* Should we really check the constraints here? Should be better, but is strange. */
6328         n = get_ASM_n_input_constraints(a);
6329         if (n != get_ASM_n_input_constraints(b))
6330                 return 1;
6331
6332         ca = get_ASM_input_constraints(a);
6333         cb = get_ASM_input_constraints(b);
6334         for (i = 0; i < n; ++i) {
6335                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6336                     || ca[i].mode != cb[i].mode)
6337                         return 1;
6338         }
6339
6340         n = get_ASM_n_output_constraints(a);
6341         if (n != get_ASM_n_output_constraints(b))
6342                 return 1;
6343
6344         ca = get_ASM_output_constraints(a);
6345         cb = get_ASM_output_constraints(b);
6346         for (i = 0; i < n; ++i) {
6347                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6348                     || ca[i].mode != cb[i].mode)
6349                         return 1;
6350         }
6351
6352         n = get_ASM_n_clobbers(a);
6353         if (n != get_ASM_n_clobbers(b))
6354                 return 1;
6355
6356         cla = get_ASM_clobbers(a);
6357         clb = get_ASM_clobbers(b);
6358         for (i = 0; i < n; ++i) {
6359                 if (cla[i] != clb[i])
6360                         return 1;
6361         }
6362
6363         return node_cmp_exception(a, b);
6364 }
6365
6366 /** Compares the inexistent attributes of two Dummy nodes. */
6367 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6368 {
6369         (void) a;
6370         (void) b;
6371         /* Dummy nodes never equal by definition */
6372         return 1;
6373 }
6374
6375 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6376 {
6377         if (get_InstOf_type(a) != get_InstOf_type(b))
6378                 return 1;
6379         return node_cmp_exception(a, b);
6380 }
6381
6382 /**
6383  * Set the default node attribute compare operation for an ir_op_ops.
6384  *
6385  * @param code   the opcode for the default operation
6386  * @param ops    the operations initialized
6387  *
6388  * @return
6389  *    The operations.
6390  */
6391 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6392 {
6393 #define CASE(a)                              \
6394         case iro_##a:                              \
6395                 ops->node_cmp_attr  = node_cmp_attr_##a; \
6396                 break
6397
6398         switch (code) {
6399         CASE(ASM);
6400         CASE(Alloc);
6401         CASE(Bound);
6402         CASE(Builtin);
6403         CASE(Call);
6404         CASE(Cast);
6405         CASE(Cmp);
6406         CASE(Confirm);
6407         CASE(Const);
6408         CASE(Conv);
6409         CASE(CopyB);
6410         CASE(Div);
6411         CASE(Dummy);
6412         CASE(Free);
6413         CASE(InstOf);
6414         CASE(Load);
6415         CASE(Mod);
6416         CASE(Phi);
6417         CASE(Proj);
6418         CASE(Sel);
6419         CASE(Store);
6420         CASE(SymConst);
6421         default:
6422                 /* leave NULL */
6423                 break;
6424         }
6425
6426         return ops;
6427 #undef CASE
6428 }  /* firm_set_default_node_cmp_attr */
6429
6430 /*
6431  * Compare function for two nodes in the value table. Gets two
6432  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
6433  */
6434 int identities_cmp(const void *elt, const void *key)
6435 {
6436         ir_node *a = (ir_node *)elt;
6437         ir_node *b = (ir_node *)key;
6438         int i, irn_arity_a;
6439
6440         if (a == b) return 0;
6441
6442         if ((get_irn_op(a) != get_irn_op(b)) ||
6443             (get_irn_mode(a) != get_irn_mode(b))) return 1;
6444
6445         /* compare if a's in and b's in are of equal length */
6446         irn_arity_a = get_irn_arity(a);
6447         if (irn_arity_a != get_irn_arity(b))
6448                 return 1;
6449
6450         /* blocks are never the same */
6451         if (is_Block(a))
6452                 return 1;
6453
6454         if (get_irn_pinned(a) == op_pin_state_pinned) {
6455                 /* for pinned nodes, the block inputs must be equal */
6456                 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6457                         return 1;
6458         } else {
6459                 ir_node *block_a = get_nodes_block(a);
6460                 ir_node *block_b = get_nodes_block(b);
6461                 if (! get_opt_global_cse()) {
6462                         /* for block-local CSE both nodes must be in the same Block */
6463                         if (block_a != block_b)
6464                                 return 1;
6465                 } else {
6466                         /* The optimistic approach would be to do nothing here.
6467                          * However doing GCSE optimistically produces a lot of partially dead code which appears
6468                          * to be worse in practice than the missed opportunities.
6469                          * So we use a very conservative variant here and only CSE if 1 value dominates the
6470                          * other. */
6471                         if (!block_dominates(block_a, block_b)
6472                             && !block_dominates(block_b, block_a))
6473                             return 1;
6474                 }
6475         }
6476
6477         /* compare a->in[0..ins] with b->in[0..ins] */
6478         for (i = 0; i < irn_arity_a; ++i) {
6479                 ir_node *pred_a = get_irn_n(a, i);
6480                 ir_node *pred_b = get_irn_n(b, i);
6481                 if (pred_a != pred_b) {
6482                         /* if both predecessors are CSE neutral they might be different */
6483                         if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6484                                 return 1;
6485                 }
6486         }
6487
6488         /*
6489          * here, we already now that the nodes are identical except their
6490          * attributes
6491          */
6492         if (a->op->ops.node_cmp_attr)
6493                 return a->op->ops.node_cmp_attr(a, b);
6494
6495         return 0;
6496 }  /* identities_cmp */
6497
6498 /*
6499  * Calculate a hash value of a node.
6500  *
6501  * @param node  The IR-node
6502  */
6503 unsigned ir_node_hash(const ir_node *node)
6504 {
6505         return node->op->ops.hash(node);
6506 }  /* ir_node_hash */
6507
6508
6509 void new_identities(ir_graph *irg)
6510 {
6511         if (irg->value_table != NULL)
6512                 del_pset(irg->value_table);
6513         irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6514 }  /* new_identities */
6515
6516 void del_identities(ir_graph *irg)
6517 {
6518         if (irg->value_table != NULL)
6519                 del_pset(irg->value_table);
6520 }  /* del_identities */
6521
6522 /* Normalize a node by putting constants (and operands with larger
6523  * node index) on the right (operator side). */
6524 void ir_normalize_node(ir_node *n)
6525 {
6526         if (is_op_commutative(get_irn_op(n))) {
6527                 ir_node *l = get_binop_left(n);
6528                 ir_node *r = get_binop_right(n);
6529
6530                 /* For commutative operators perform  a OP b == b OP a but keep
6531                  * constants on the RIGHT side. This helps greatly in some
6532                  * optimizations.  Moreover we use the idx number to make the form
6533                  * deterministic. */
6534                 if (!operands_are_normalized(l, r)) {
6535                         set_binop_left(n, r);
6536                         set_binop_right(n, l);
6537                         hook_normalize(n);
6538                 }
6539         }
6540 }  /* ir_normalize_node */
6541
6542 /*
6543  * Return the canonical node computing the same value as n.
6544  * Looks up the node in a hash table, enters it in the table
6545  * if it isn't there yet.
6546  *
6547  * @param n            the node to look up
6548  *
6549  * @return a node that computes the same value as n or n if no such
6550  *         node could be found
6551  */
6552 ir_node *identify_remember(ir_node *n)
6553 {
6554         ir_graph *irg         = get_irn_irg(n);
6555         pset     *value_table = irg->value_table;
6556         ir_node  *nn;
6557
6558         if (value_table == NULL)
6559                 return n;
6560
6561         ir_normalize_node(n);
6562         /* lookup or insert in hash table with given hash key. */
6563         nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6564
6565         if (nn != n) {
6566                 /* n is reachable again */
6567                 edges_node_revival(nn);
6568         }
6569
6570         return nn;
6571 }  /* identify_remember */
6572
6573 /**
6574  * During construction we set the op_pin_state_pinned flag in the graph right
6575  * when the optimization is performed.  The flag turning on procedure global
6576  * cse could be changed between two allocations.  This way we are safe.
6577  *
6578  * @param n            The node to lookup
6579  */
6580 static inline ir_node *identify_cons(ir_node *n)
6581 {
6582         ir_node *old = n;
6583
6584         n = identify_remember(n);
6585         if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6586                 ir_graph *irg = get_irn_irg(n);
6587                 set_irg_pinned(irg, op_pin_state_floats);
6588         }
6589         return n;
6590 }  /* identify_cons */
6591
6592 /* Add a node to the identities value table. */
6593 void add_identities(ir_node *node)
6594 {
6595         if (!get_opt_cse())
6596                 return;
6597         if (is_Block(node))
6598                 return;
6599
6600         identify_remember(node);
6601 }
6602
6603 /* Visit each node in the value table of a graph. */
6604 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6605 {
6606         ir_node  *node;
6607         ir_graph *rem = current_ir_graph;
6608
6609         current_ir_graph = irg;
6610         foreach_pset(irg->value_table, ir_node*, node) {
6611                 visit(node, env);
6612         }
6613         current_ir_graph = rem;
6614 }  /* visit_all_identities */
6615
6616 /**
6617  * These optimizations deallocate nodes from the obstack.
6618  * It can only be called if it is guaranteed that no other nodes
6619  * reference this one, i.e., right after construction of a node.
6620  *
6621  * @param n   The node to optimize
6622  */
6623 ir_node *optimize_node(ir_node *n)
6624 {
6625         ir_node   *oldn = n;
6626         ir_graph  *irg  = get_irn_irg(n);
6627         unsigned   iro  = get_irn_opcode(n);
6628         ir_tarval *tv;
6629
6630         /* Always optimize Phi nodes: part of the construction. */
6631         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6632
6633         /* constant expression evaluation / constant folding */
6634         if (get_opt_constant_folding()) {
6635                 /* neither constants nor Tuple values can be evaluated */
6636                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6637                         /* try to evaluate */
6638                         tv = computed_value(n);
6639                         if (tv != tarval_bad) {
6640                                 ir_node *nw;
6641                                 size_t node_size;
6642
6643                                 /*
6644                                  * we MUST copy the node here temporarily, because it's still
6645                                  * needed for DBG_OPT_CSTEVAL
6646                                  */
6647                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
6648                                 oldn = (ir_node*)alloca(node_size);
6649
6650                                 memcpy(oldn, n, node_size);
6651                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6652
6653                                 /* ARG, copy the in array, we need it for statistics */
6654                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6655
6656                                 /* note the inplace edges module */
6657                                 edges_node_deleted(n);
6658
6659                                 /* evaluation was successful -- replace the node. */
6660                                 irg_kill_node(irg, n);
6661                                 nw = new_r_Const(irg, tv);
6662
6663                                 DBG_OPT_CSTEVAL(oldn, nw);
6664                                 return nw;
6665                         }
6666                 }
6667         }
6668
6669         /* remove unnecessary nodes */
6670         if (get_opt_algebraic_simplification() ||
6671             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6672             (iro == iro_Id)   ||
6673             (iro == iro_Proj) ||
6674             (iro == iro_Block)  )  /* Flags tested local. */
6675                 n = equivalent_node(n);
6676
6677         /* Common Subexpression Elimination.
6678          *
6679          * Checks whether n is already available.
6680          * The block input is used to distinguish different subexpressions. Right
6681          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6682          * subexpressions within a block.
6683          */
6684         if (get_opt_cse())
6685                 n = identify_cons(n);
6686
6687         if (n != oldn) {
6688                 edges_node_deleted(oldn);
6689
6690                 /* We found an existing, better node, so we can deallocate the old node. */
6691                 irg_kill_node(irg, oldn);
6692                 return n;
6693         }
6694
6695         /* Some more constant expression evaluation that does not allow to
6696            free the node. */
6697         iro = get_irn_opcode(n);
6698         if (get_opt_algebraic_simplification() ||
6699             (iro == iro_Cond) ||
6700             (iro == iro_Proj))     /* Flags tested local. */
6701                 n = transform_node(n);
6702
6703         /* Now we have a legal, useful node. Enter it in hash table for CSE */
6704         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6705                 ir_node *o = n;
6706                 n = identify_remember(o);
6707                 if (o != n)
6708                         DBG_OPT_CSE(o, n);
6709         }
6710
6711         return n;
6712 }  /* optimize_node */
6713
6714
6715 /**
6716  * These optimizations never deallocate nodes (in place).  This can cause dead
6717  * nodes lying on the obstack.  Remove these by a dead node elimination,
6718  * i.e., a copying garbage collection.
6719  */
6720 ir_node *optimize_in_place_2(ir_node *n)
6721 {
6722         ir_tarval *tv;
6723         ir_node   *oldn = n;
6724         unsigned   iro  = get_irn_opcode(n);
6725
6726         if (!get_opt_optimize() && !is_Phi(n)) return n;
6727
6728         if (iro == iro_Deleted)
6729                 return n;
6730
6731         /* constant expression evaluation / constant folding */
6732         if (get_opt_constant_folding()) {
6733                 /* neither constants nor Tuple values can be evaluated */
6734                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6735                         /* try to evaluate */
6736                         tv = computed_value(n);
6737                         if (tv != tarval_bad) {
6738                                 /* evaluation was successful -- replace the node. */
6739                                 ir_graph *irg = get_irn_irg(n);
6740
6741                                 n = new_r_Const(irg, tv);
6742
6743                                 DBG_OPT_CSTEVAL(oldn, n);
6744                                 return n;
6745                         }
6746                 }
6747         }
6748
6749         /* remove unnecessary nodes */
6750         if (get_opt_constant_folding() ||
6751             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6752             (iro == iro_Id)   ||   /* ... */
6753             (iro == iro_Proj) ||   /* ... */
6754             (iro == iro_Block)  )  /* Flags tested local. */
6755                 n = equivalent_node(n);
6756
6757         /** common subexpression elimination **/
6758         /* Checks whether n is already available. */
6759         /* The block input is used to distinguish different subexpressions.  Right
6760            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6761            subexpressions within a block. */
6762         if (get_opt_cse()) {
6763                 ir_node *o = n;
6764                 n = identify_remember(o);
6765                 if (o != n)
6766                         DBG_OPT_CSE(o, n);
6767         }
6768
6769         /* Some more constant expression evaluation. */
6770         iro = get_irn_opcode(n);
6771         if (get_opt_constant_folding() ||
6772                 (iro == iro_Cond) ||
6773                 (iro == iro_Proj))     /* Flags tested local. */
6774                 n = transform_node(n);
6775
6776         /* Now we can verify the node, as it has no dead inputs any more. */
6777         irn_verify(n);
6778
6779         /* Now we have a legal, useful node. Enter it in hash table for cse.
6780            Blocks should be unique anyways.  (Except the successor of start:
6781            is cse with the start block!) */
6782         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6783                 ir_node *o = n;
6784                 n = identify_remember(o);
6785                 if (o != n)
6786                         DBG_OPT_CSE(o, n);
6787         }
6788
6789         return n;
6790 }  /* optimize_in_place_2 */
6791
6792 /**
6793  * Wrapper for external use, set proper status bits after optimization.
6794  */
6795 ir_node *optimize_in_place(ir_node *n)
6796 {
6797         ir_graph *irg = get_irn_irg(n);
6798         /* Handle graph state */
6799         assert(get_irg_phase_state(irg) != phase_building);
6800
6801         if (get_opt_global_cse())
6802                 set_irg_pinned(irg, op_pin_state_floats);
6803
6804         /* FIXME: Maybe we could also test whether optimizing the node can
6805            change the control graph. */
6806         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
6807         return optimize_in_place_2(n);
6808 }  /* optimize_in_place */
6809
6810 /**
6811  * Calculate a hash value of a Const node.
6812  */
6813 static unsigned hash_Const(const ir_node *node)
6814 {
6815         unsigned h;
6816
6817         /* special value for const, as they only differ in their tarval. */
6818         h = HASH_PTR(node->attr.con.tarval);
6819
6820         return h;
6821 }  /* hash_Const */
6822
6823 /**
6824  * Calculate a hash value of a SymConst node.
6825  */
6826 static unsigned hash_SymConst(const ir_node *node)
6827 {
6828         unsigned h;
6829
6830         /* all others are pointers */
6831         h = HASH_PTR(node->attr.symc.sym.type_p);
6832
6833         return h;
6834 }  /* hash_SymConst */
6835
6836 /**
6837  * Set the default hash operation in an ir_op_ops.
6838  *
6839  * @param code   the opcode for the default operation
6840  * @param ops    the operations initialized
6841  *
6842  * @return
6843  *    The operations.
6844  */
6845 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6846 {
6847 #define CASE(a)                                    \
6848         case iro_##a:                                  \
6849                 ops->hash  = hash_##a; \
6850                 break
6851
6852         /* hash function already set */
6853         if (ops->hash != NULL)
6854                 return ops;
6855
6856         switch (code) {
6857         CASE(Const);
6858         CASE(SymConst);
6859         default:
6860                 /* use input/mode default hash if no function was given */
6861                 ops->hash = firm_default_hash;
6862         }
6863
6864         return ops;
6865 #undef CASE
6866 }
6867
6868 /*
6869  * Sets the default operation for an ir_ops.
6870  */
6871 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6872 {
6873         ops = firm_set_default_hash(code, ops);
6874         ops = firm_set_default_computed_value(code, ops);
6875         ops = firm_set_default_equivalent_node(code, ops);
6876         ops = firm_set_default_transform_node(code, ops);
6877         ops = firm_set_default_node_cmp_attr(code, ops);
6878         ops = firm_set_default_get_type_attr(code, ops);
6879         ops = firm_set_default_get_entity_attr(code, ops);
6880
6881         return ops;
6882 }  /* firm_set_default_operations */