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