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