6b3e6b4187810cd51166aff5f6eadbea1e9e20f3
[libfirm] / ir / ir / iropt.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/iropt.c
4  * Purpose:     iropt --- optimizations intertwined with IR construction.
5  * Author:      Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_ALLOCA_H
18 #include <alloca.h>
19 #endif
20 #ifdef HAVE_MALLOC_H
21 #include <malloc.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26
27 # include "irnode_t.h"
28 # include "irgraph_t.h"
29 # include "irmode_t.h"
30 # include "iropt_t.h"
31 # include "ircons_t.h"
32 # include "irgmod.h"
33 # include "irvrfy.h"
34 # include "tv_t.h"
35 # include "dbginfo_t.h"
36 # include "iropt_dbg.h"
37 # include "irflag_t.h"
38 # include "firmstat.h"
39 # include "irarch.h"
40 # include "hashptr.h"
41
42 /* Make types visible to allow most efficient access */
43 # include "entity_t.h"
44
45 # ifdef DO_HEAPANALYSIS
46 /* heapanal can't cope with NoMems */
47 # else /* if defined DO_HEAPANALYSIS */
48 #  define USE_NOMEM
49 # endif /* defined DO_HEAPANALYSIS */
50
51 /**
52  * Trivial INLINEable routine for copy propagation.
53  * Does follow Ids, needed to optimize INLINEd code.
54  */
55 static INLINE ir_node *
56 follow_Id (ir_node *n)
57 {
58   while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
59   return n;
60 }
61
62 /**
63  * return the value of a Constant
64  */
65 static tarval *computed_value_Const(ir_node *n)
66 {
67     return get_Const_tarval(n);
68 }
69
70 /**
71  * return the value of a 'sizeof' SymConst
72  */
73 static tarval *computed_value_SymConst(ir_node *n)
74 {
75   if ((get_SymConst_kind(n) == symconst_size) &&
76       (get_type_state(get_SymConst_type(n))) == layout_fixed)
77     return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
78   return tarval_bad;
79 }
80
81 /**
82  * return the value of an Add
83  */
84 static tarval *computed_value_Add(ir_node *n)
85 {
86   ir_node *a = get_Add_left(n);
87   ir_node *b = get_Add_right(n);
88
89   tarval *ta = value_of(a);
90   tarval *tb = value_of(b);
91
92   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
93     return tarval_add(ta, tb);
94
95   return tarval_bad;
96 }
97
98 /**
99  * return the value of a Sub
100  * Special case: a - a
101  */
102 static tarval *computed_value_Sub(ir_node *n)
103 {
104   ir_node *a = get_Sub_left(n);
105   ir_node *b = get_Sub_right(n);
106   tarval *ta;
107   tarval *tb;
108
109   /* a - a */
110   if (a == b && !is_Bad(a))
111     return get_tarval_null(get_irn_mode(n));
112
113   ta = value_of(a);
114   tb = value_of(b);
115
116   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
117     return tarval_sub(ta, tb);
118
119   return tarval_bad;
120 }
121
122 /**
123  * return the value of an unary Minus
124  */
125 static tarval *computed_value_Minus(ir_node *n)
126 {
127   ir_node *a = get_Minus_op(n);
128   tarval *ta = value_of(a);
129
130   if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
131     return tarval_neg(ta);
132
133   return tarval_bad;
134 }
135
136 /**
137  * return the value of a Mul
138  */
139 static tarval *computed_value_Mul(ir_node *n)
140 {
141   ir_node *a = get_Mul_left(n);
142   ir_node *b = get_Mul_right(n);
143
144   tarval *ta = value_of(a);
145   tarval *tb = value_of(b);
146
147   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
148     return tarval_mul(ta, tb);
149   } else {
150     /* a*0 = 0 or 0*b = 0:
151        calls computed_value recursive and returns the 0 with proper
152        mode. */
153     if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
154       return ta;
155     if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
156       return tb;
157   }
158   return tarval_bad;
159 }
160
161 /**
162  * return the value of a floating point Quot
163  */
164 static tarval *computed_value_Quot(ir_node *n)
165 {
166   ir_node *a = get_Quot_left(n);
167   ir_node *b = get_Quot_right(n);
168
169   tarval *ta = value_of(a);
170   tarval *tb = value_of(b);
171
172   /* This was missing in original implementation. Why? */
173   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
174     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
175       return tarval_quo(ta, tb);
176   }
177   return tarval_bad;
178 }
179
180 /**
181  * calculate the value of an integer Div of two nodes
182  * Special case: 0 / b
183  */
184 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
185 {
186   tarval *ta = value_of(a);
187   tarval *tb = value_of(b);
188
189   /* Compute c1 / c2 or 0 / a, a != 0 */
190   if (ta != tarval_bad) {
191     if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
192       return tarval_div(ta, tb);
193     else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
194       return ta;
195   }
196   return tarval_bad;
197 }
198
199 /**
200  * return the value of an integer Div
201  */
202 static tarval *computed_value_Div(ir_node *n)
203 {
204   return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
205 }
206
207 /**
208  * calculate the value of an integer Mod of two nodes
209  * Special case: a % 1
210  */
211 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
212 {
213   tarval *ta = value_of(a);
214   tarval *tb = value_of(b);
215
216   /* Compute c1 % c2 or a % 1 */
217   if (tb != tarval_bad) {
218     if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
219       return tarval_mod(ta, tb);
220     else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
221       return get_mode_null(get_irn_mode(a));
222   }
223
224   return tarval_bad;
225 }
226
227 /**
228  * return the value of an integer Mod
229  */
230 static tarval *computed_value_Mod(ir_node *n)
231 {
232   return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
233 }
234
235 /**
236  * return the value of an Abs
237  */
238 static tarval *computed_value_Abs(ir_node *n)
239 {
240   ir_node *a = get_Abs_op(n);
241   tarval *ta = value_of(a);
242
243   if (ta != tarval_bad)
244     return tarval_abs(ta);
245
246   return tarval_bad;
247 }
248
249 /**
250  * return the value of an And
251  * Special case: a & 0, 0 & b
252  */
253 static tarval *computed_value_And(ir_node *n)
254 {
255   ir_node *a = get_And_left(n);
256   ir_node *b = get_And_right(n);
257
258   tarval *ta = value_of(a);
259   tarval *tb = value_of(b);
260
261   if ((ta != tarval_bad) && (tb != tarval_bad)) {
262     return tarval_and (ta, tb);
263   } else {
264     tarval *v;
265
266     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
267         || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
268       return v;
269     }
270   }
271   return tarval_bad;
272 }
273
274 /**
275  * return the value of an Or
276  * Special case: a | 1...1, 1...1 | b
277  */
278 static tarval *computed_value_Or(ir_node *n)
279 {
280   ir_node *a = get_Or_left(n);
281   ir_node *b = get_Or_right(n);
282
283   tarval *ta = value_of(a);
284   tarval *tb = value_of(b);
285
286   if ((ta != tarval_bad) && (tb != tarval_bad)) {
287     return tarval_or (ta, tb);
288   } else {
289     tarval *v;
290     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
291         || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
292       return v;
293     }
294   }
295   return tarval_bad;
296 }
297
298 /**
299  * return the value of an Eor
300  */
301 static tarval *computed_value_Eor(ir_node *n)
302 {
303   ir_node *a = get_Eor_left(n);
304   ir_node *b = get_Eor_right(n);
305
306   tarval *ta, *tb;
307
308   if (a == b)
309     return get_tarval_null(get_irn_mode(n));
310
311   ta = value_of(a);
312   tb = value_of(b);
313
314   if ((ta != tarval_bad) && (tb != tarval_bad)) {
315     return tarval_eor (ta, tb);
316   }
317   return tarval_bad;
318 }
319
320 /**
321  * return the value of a Not
322  */
323 static tarval *computed_value_Not(ir_node *n)
324 {
325   ir_node *a = get_Not_op(n);
326   tarval *ta = value_of(a);
327
328   if (ta != tarval_bad)
329     return tarval_not(ta);
330
331   return tarval_bad;
332 }
333
334 /**
335  * return the value of a Shl
336  */
337 static tarval *computed_value_Shl(ir_node *n)
338 {
339   ir_node *a = get_Shl_left(n);
340   ir_node *b = get_Shl_right(n);
341
342   tarval *ta = value_of(a);
343   tarval *tb = value_of(b);
344
345   if ((ta != tarval_bad) && (tb != tarval_bad)) {
346     return tarval_shl (ta, tb);
347   }
348   return tarval_bad;
349 }
350
351 /**
352  * return the value of a Shr
353  */
354 static tarval *computed_value_Shr(ir_node *n)
355 {
356   ir_node *a = get_Shr_left(n);
357   ir_node *b = get_Shr_right(n);
358
359   tarval *ta = value_of(a);
360   tarval *tb = value_of(b);
361
362   if ((ta != tarval_bad) && (tb != tarval_bad)) {
363     return tarval_shr (ta, tb);
364   }
365   return tarval_bad;
366 }
367
368 /**
369  * return the value of a Shrs
370  */
371 static tarval *computed_value_Shrs(ir_node *n)
372 {
373   ir_node *a = get_Shrs_left(n);
374   ir_node *b = get_Shrs_right(n);
375
376   tarval *ta = value_of(a);
377   tarval *tb = value_of(b);
378
379   if ((ta != tarval_bad) && (tb != tarval_bad)) {
380     return tarval_shrs (ta, tb);
381   }
382   return tarval_bad;
383 }
384
385 /**
386  * return the value of a Rot
387  */
388 static tarval *computed_value_Rot(ir_node *n)
389 {
390   ir_node *a = get_Rot_left(n);
391   ir_node *b = get_Rot_right(n);
392
393   tarval *ta = value_of(a);
394   tarval *tb = value_of(b);
395
396   if ((ta != tarval_bad) && (tb != tarval_bad)) {
397     return tarval_rot (ta, tb);
398   }
399   return tarval_bad;
400 }
401
402 /**
403  * return the value of a Conv
404  */
405 static tarval *computed_value_Conv(ir_node *n)
406 {
407   ir_node *a = get_Conv_op(n);
408   tarval *ta = value_of(a);
409
410   if (ta != tarval_bad)
411     return tarval_convert_to(ta, get_irn_mode(n));
412
413   return tarval_bad;
414 }
415
416 /**
417  * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
418  */
419 static tarval *computed_value_Proj(ir_node *n)
420 {
421   ir_node *a = get_Proj_pred(n);
422   ir_node *aa, *ab;
423   long proj_nr;
424
425   /* Optimize Cmp nodes.
426      This performs a first step of unreachable code elimination.
427      Proj can not be computed, but folding a Cmp above the Proj here is
428      not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
429      only 1 is used.
430      There are several case where we can evaluate a Cmp node:
431      1. The nodes compared are both the same.  If we compare for
432         equal, greater equal, ... this will return true, else it
433         will return false.  This step relies on cse.
434      2. The predecessors of Cmp are target values.  We can evaluate
435         the Cmp.
436      3. The predecessors are Allocs or void* constants.  Allocs never
437         return NULL, they raise an exception.   Therefore we can predict
438         the Cmp result. */
439   switch (get_irn_opcode(a)) {
440   case iro_Cmp:
441     aa = get_Cmp_left(a);
442     ab = get_Cmp_right(a);
443     proj_nr = get_Proj_proj(n);
444
445     if (aa == ab && !mode_is_float(get_irn_mode(aa))) { /* 1.: */
446       /* BEWARE: a == a is NOT always True for floating Point!!! */
447       /* This is a trick with the bits used for encoding the Cmp
448          Proj numbers, the following statement is not the same:
449       return new_tarval_from_long (proj_nr == Eq, mode_b) */
450       return new_tarval_from_long (proj_nr & Eq, mode_b);
451     } else {
452       tarval *taa = value_of(aa);
453       tarval *tab = value_of(ab);
454
455       if ((taa != tarval_bad) && (tab != tarval_bad)) { /* 2.: */
456         /* strange checks... */
457         pnc_number flags = tarval_cmp (taa, tab);
458         if (flags != False) {
459           return new_tarval_from_long (proj_nr & flags, mode_b);
460         }
461       } else {  /* check for 3.: */
462         ir_node *aaa = skip_Id(skip_Proj(aa));
463         ir_node *aba = skip_Id(skip_Proj(ab));
464
465         if (   (   (/* aa is ProjP and aaa is Alloc */
466                        (get_irn_op(aa) == op_Proj)
467                     && (mode_is_reference(get_irn_mode(aa)))
468                     && (get_irn_op(aaa) == op_Alloc))
469                 && (   (/* ab is constant void */
470                            (get_irn_op(ab) == op_Const)
471                         && (mode_is_reference(get_irn_mode(ab)))
472                         && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
473                     || (/* ab is other Alloc */
474                            (get_irn_op(ab) == op_Proj)
475                         && (mode_is_reference(get_irn_mode(ab)))
476                         && (get_irn_op(aba) == op_Alloc)
477                         && (aaa != aba))))
478             || (/* aa is void and aba is Alloc */
479                    (get_irn_op(aa) == op_Const)
480                 && (mode_is_reference(get_irn_mode(aa)))
481                 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
482                 && (get_irn_op(ab) == op_Proj)
483                 && (mode_is_reference(get_irn_mode(ab)))
484                 && (get_irn_op(aba) == op_Alloc)))
485           /* 3.: */
486           return new_tarval_from_long (proj_nr & Ne, mode_b);
487       }
488     }
489     break;
490
491   case iro_DivMod:
492     /* compute either the Div or the Mod part */
493     proj_nr = get_Proj_proj(n);
494     if (proj_nr == pn_DivMod_res_div)
495       return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
496     else if (proj_nr == pn_DivMod_res_mod)
497       return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
498     break;
499
500   case iro_Div:
501     if (get_Proj_proj(n) == pn_Div_res)
502       return computed_value(a);
503     break;
504
505   case iro_Mod:
506     if (get_Proj_proj(n) == pn_Mod_res)
507       return computed_value(a);
508     break;
509
510   default:
511     return tarval_bad;
512   }
513   return tarval_bad;
514 }
515
516 /**
517  * If the parameter n can be computed, return its value, else tarval_bad.
518  * Performs constant folding.
519  *
520  * @param n  The node this should be evaluated
521  */
522 tarval *computed_value(ir_node *n)
523 {
524   if (n->op->computed_value)
525     return n->op->computed_value(n);
526   return tarval_bad;
527 }
528
529 /**
530  * set the default computed_value evaluator
531  */
532 static ir_op *firm_set_default_computed_value(ir_op *op)
533 {
534 #define CASE(a)                               \
535   case iro_##a:                               \
536     op->computed_value  = computed_value_##a; \
537     break
538
539   switch (op->code) {
540   CASE(Const);
541   CASE(SymConst);
542   CASE(Add);
543   CASE(Sub);
544   CASE(Minus);
545   CASE(Mul);
546   CASE(Quot);
547   CASE(Div);
548   CASE(Mod);
549   CASE(Abs);
550   CASE(And);
551   CASE(Or);
552   CASE(Eor);
553   CASE(Not);
554   CASE(Shl);
555   CASE(Shr);
556   CASE(Shrs);
557   CASE(Rot);
558   CASE(Conv);
559   CASE(Proj);
560   default:
561     op->computed_value  = NULL;
562   }
563
564   return op;
565 #undef CASE
566 }
567
568 #if 0
569 /* returns 1 if the a and b are pointers to different locations. */
570 static bool
571 different_identity (ir_node *a, ir_node *b)
572 {
573   assert (mode_is_reference(get_irn_mode (a))
574           && mode_is_reference(get_irn_mode (b)));
575
576   if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
577     ir_node *a1 = get_Proj_pred (a);
578     ir_node *b1 = get_Proj_pred (b);
579     if (a1 != b1 && get_irn_op (a1) == op_Alloc
580                 && get_irn_op (b1) == op_Alloc)
581       return 1;
582   }
583   return 0;
584 }
585 #endif
586
587 static ir_node *equivalent_node_Block(ir_node *n)
588 {
589   ir_node *oldn = n;
590
591   /* The Block constructor does not call optimize, but mature_immBlock
592      calls the optimization. */
593   assert(get_Block_matured(n));
594
595   /* Straightening: a single entry Block following a single exit Block
596      can be merged, if it is not the Start block. */
597   /* !!! Beware, all Phi-nodes of n must have been optimized away.
598      This should be true, as the block is matured before optimize is called.
599      But what about Phi-cycles with the Phi0/Id that could not be resolved?
600      Remaining Phi nodes are just Ids. */
601    if ((get_Block_n_cfgpreds(n) == 1) &&
602        (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
603      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
604      if (predblock == oldn) {
605        /* Jmp jumps into the block it is in -- deal self cycle. */
606        n = set_Block_dead(n);
607        DBG_OPT_DEAD(oldn, n);
608      } else if (get_opt_control_flow_straightening()) {
609        n = predblock;
610        DBG_OPT_STG(oldn, n);
611      }
612    }
613    else if ((get_Block_n_cfgpreds(n) == 1) &&
614             (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
615      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
616      if (predblock == oldn) {
617        /* Jmp jumps into the block it is in -- deal self cycle. */
618        n = set_Block_dead(n);
619        DBG_OPT_DEAD(oldn, n);
620      }
621    }
622    else if ((get_Block_n_cfgpreds(n) == 2) &&
623             (get_opt_control_flow_weak_simplification())) {
624     /* Test whether Cond jumps twice to this block
625        @@@ we could do this also with two loops finding two preds from several ones. */
626     ir_node *a = get_Block_cfgpred(n, 0);
627     ir_node *b = get_Block_cfgpred(n, 1);
628
629     if ((get_irn_op(a) == op_Proj) &&
630         (get_irn_op(b) == op_Proj) &&
631         (get_Proj_pred(a) == get_Proj_pred(b)) &&
632         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
633         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
634       /* Also a single entry Block following a single exit Block.  Phis have
635          twice the same operand and will be optimized away. */
636       n = get_nodes_block(a);
637       DBG_OPT_IFSIM(oldn, a, b, n);
638     }
639   } else if (get_opt_unreachable_code() &&
640              (n != current_ir_graph->start_block) &&
641              (n != current_ir_graph->end_block)     ) {
642     int i, n_cfg = get_Block_n_cfgpreds(n);
643
644     /* If all inputs are dead, this block is dead too, except if it is
645        the start or end block.  This is a step of unreachable code
646        elimination */
647     for (i = 0; i < n_cfg; i++) {
648       ir_node *pred = get_Block_cfgpred(n, i);
649       ir_node *pred_blk;
650
651       if (is_Bad(pred)) continue;
652       pred_blk = get_nodes_block(pred);
653
654       if (is_Block_dead(pred_blk)) continue;
655
656       if (pred_blk != n) {
657         /* really found a living input */
658         break;
659       }
660     }
661     if (i == n_cfg)
662       n = set_Block_dead(n);
663   }
664
665   return n;
666 }
667
668 /**
669  * Returns a equivalent node for a Jmp, a Bad :-)
670  * Of course this only happens if the Block of the Jmp is Bad.
671  */
672 static ir_node *equivalent_node_Jmp(ir_node *n)
673 {
674   /* GL: Why not same for op_Raise?? */
675   /* unreachable code elimination */
676   if (is_Block_dead(get_nodes_block(n)))
677     n = new_Bad();
678
679   return n;
680 }
681
682 static ir_node *equivalent_node_Cond(ir_node *n)
683 {
684   /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
685      See cases for iro_Cond and iro_Proj in transform_node. */
686   return n;
687 }
688
689 /**
690  * optimize operations that are commutative and have neutral 0,
691  * so a op 0 = 0 op a = a.
692  */
693 static ir_node *equivalent_node_neutral_zero(ir_node *n)
694 {
695   ir_node *oldn = n;
696
697   ir_node *a = get_binop_left(n);
698   ir_node *b = get_binop_right(n);
699
700   tarval *tv;
701   ir_node *on;
702
703   /* After running compute_node there is only one constant predecessor.
704      Find this predecessors value and remember the other node: */
705   if ((tv = value_of(a)) != tarval_bad) {
706     on = b;
707   } else if ((tv = value_of(b)) != tarval_bad) {
708     on = a;
709   } else
710     return n;
711
712   /* If this predecessors constant value is zero, the operation is
713      unnecessary. Remove it: */
714   if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
715     n = on;
716
717     DBG_OPT_ALGSIM1(oldn, a, b, n);
718   }
719
720   return n;
721 }
722
723 #define equivalent_node_Add  equivalent_node_neutral_zero
724 #define equivalent_node_Eor  equivalent_node_neutral_zero
725
726 /**
727  * optimize operations that are not commutative but have neutral 0 on left,
728  * so a op 0 = a.
729  */
730 static ir_node *equivalent_node_left_zero(ir_node *n)
731 {
732   ir_node *oldn = n;
733
734   ir_node *a = get_binop_left(n);
735   ir_node *b = get_binop_right(n);
736
737   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
738     n = a;
739
740     DBG_OPT_ALGSIM1(oldn, a, b, n);
741   }
742
743   return n;
744 }
745
746 #define equivalent_node_Sub   equivalent_node_left_zero
747 #define equivalent_node_Shl   equivalent_node_left_zero
748 #define equivalent_node_Shr   equivalent_node_left_zero
749 #define equivalent_node_Shrs  equivalent_node_left_zero
750 #define equivalent_node_Rot   equivalent_node_left_zero
751
752 /**
753  * Er, a "symmetic unop", ie op(op(n)) = n.
754  */
755 static ir_node *equivalent_node_symmetric_unop(ir_node *n)
756 {
757   ir_node *oldn = n;
758   ir_node *pred = get_unop_op(n);
759
760   /* optimize symmetric unop */
761   if (get_irn_op(pred) == get_irn_op(n)) {
762     n = get_unop_op(pred);
763     DBG_OPT_ALGSIM2(oldn, pred, n);
764   }
765   return n;
766 }
767
768 /* NotNot x == x */
769 #define equivalent_node_Not    equivalent_node_symmetric_unop
770
771 /* --x == x */  /* ??? Is this possible or can --x raise an
772                        out of bounds exception if min =! max? */
773 #define equivalent_node_Minus  equivalent_node_symmetric_unop
774
775 /**
776  * Optimize a * 1 = 1 * a = a.
777  */
778 static ir_node *equivalent_node_Mul(ir_node *n)
779 {
780   ir_node *oldn = n;
781
782   ir_node *a = get_Mul_left(n);
783   ir_node *b = get_Mul_right(n);
784
785   /* Mul is commutative and has again an other neutral element. */
786   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
787     n = b;
788     DBG_OPT_ALGSIM1(oldn, a, b, n);
789   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
790     n = a;
791     DBG_OPT_ALGSIM1(oldn, a, b, n);
792   }
793   return n;
794 }
795
796 /**
797  * Optimize a / 1 = a.
798  */
799 static ir_node *equivalent_node_Div(ir_node *n)
800 {
801   ir_node *a = get_Div_left(n);
802   ir_node *b = get_Div_right(n);
803
804   /* Div is not commutative. */
805   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
806     /* Turn Div into a tuple (mem, bad, a) */
807     ir_node *mem = get_Div_mem(n);
808     turn_into_tuple(n, 3);
809     set_Tuple_pred(n, pn_Div_M,        mem);
810     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
811     set_Tuple_pred(n, pn_Div_res,      a);
812   }
813   return n;
814 }
815
816 /**
817  * Optimize a / 1 = a.
818  */
819 static ir_node *equivalent_node_DivMod(ir_node *n)
820 {
821   ir_node *a = get_DivMod_left(n);
822   ir_node *b = get_DivMod_right(n);
823
824   /* Div is not commutative. */
825   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
826     /* Turn DivMod into a tuple (mem, bad, a, 0) */
827     ir_node *mem = get_Div_mem(n);
828     ir_mode *mode = get_irn_mode(b);
829
830     turn_into_tuple(n, 4);
831     set_Tuple_pred(n, pn_DivMod_M,        mem);
832     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
833     set_Tuple_pred(n, pn_DivMod_res_div,  a);
834     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
835   }
836   return n;
837 }
838
839 /**
840  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
841  */
842 static ir_node *equivalent_node_Or(ir_node *n)
843 {
844   ir_node *oldn = n;
845
846   ir_node *a = get_Or_left(n);
847   ir_node *b = get_Or_right(n);
848
849   if (a == b) {
850     n = a;    /* Or has it's own neutral element */
851   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
852     n = b;
853     DBG_OPT_ALGSIM1(oldn, a, b, n);
854   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
855     n = a;
856     DBG_OPT_ALGSIM1(oldn, a, b, n);
857   }
858
859   return n;
860 }
861
862 /**
863  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
864  */
865 static ir_node *equivalent_node_And(ir_node *n)
866 {
867   ir_node *oldn = n;
868
869   ir_node *a = get_And_left(n);
870   ir_node *b = get_And_right(n);
871
872   if (a == b) {
873     n = a;    /* And has it's own neutral element */
874   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
875     n = b;
876     DBG_OPT_ALGSIM1(oldn, a, b, n);
877   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
878     n = a;
879     DBG_OPT_ALGSIM1(oldn, a, b, n);
880   }
881   return n;
882 }
883
884 /**
885  * Try to remove useless conv's:
886  */
887 static ir_node *equivalent_node_Conv(ir_node *n)
888 {
889   ir_node *oldn = n;
890   ir_node *a = get_Conv_op(n);
891   ir_node *b;
892
893   ir_mode *n_mode = get_irn_mode(n);
894   ir_mode *a_mode = get_irn_mode(a);
895
896   if (n_mode == a_mode) { /* No Conv necessary */
897     n = a;
898     DBG_OPT_ALGSIM3(oldn, a, n);
899   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
900     ir_mode *b_mode;
901
902     b = get_Conv_op(a);
903     n_mode = get_irn_mode(n);
904     b_mode = get_irn_mode(b);
905
906     if (n_mode == b_mode) {
907       if (n_mode == mode_b) {
908         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
909     DBG_OPT_ALGSIM1(oldn, a, b, n);
910       }
911       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
912         if (smaller_mode(b_mode, a_mode)){
913           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
914       DBG_OPT_ALGSIM1(oldn, a, b, n);
915         }
916       }
917     }
918   }
919   return n;
920 }
921
922 /**
923  * A Cast may be removed if the type of the previous node
924  * is already to type of the Cast.
925  */
926 static ir_node *equivalent_node_Cast(ir_node *n) {
927   ir_node *pred = get_Cast_op(n);
928   if (get_irn_type(pred) == get_Cast_type(n))
929     n = pred;
930   return n;
931 }
932
933 /* Several optimizations:
934    - no Phi in start block.
935    - remove Id operators that are inputs to Phi
936    - fold Phi-nodes, iff they have only one predecessor except
937            themselves.
938 */
939 static ir_node *equivalent_node_Phi(ir_node *n)
940 {
941   int i, n_preds;
942
943   ir_node *oldn = n;
944   ir_node *block = NULL;     /* to shutup gcc */
945   ir_node *first_val = NULL; /* to shutup gcc */
946   ir_node *scnd_val = NULL;  /* to shutup gcc */
947
948   if (!get_opt_normalize()) return n;
949
950   n_preds = get_Phi_n_preds(n);
951
952   block = get_nodes_block(n);
953   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
954      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
955   if ((is_Block_dead(block)) ||                  /* Control dead */
956       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
957     return new_Bad();                            /* in the Start Block. */
958
959   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
960
961 #if 0
962   /* first we test for a special case: */
963   /* Confirm is a special node fixing additional information for a
964      value that is known at a certain point.  This is useful for
965      dataflow analysis. */
966   if (n_preds == 2) {
967     ir_node *a = get_Phi_pred(n, 0);
968     ir_node *b = get_Phi_pred(n, 1);
969     if (   (get_irn_op(a) == op_Confirm)
970         && (get_irn_op(b) == op_Confirm)
971         && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
972         && (get_irn_n(a, 1) == get_irn_n (b, 1))
973         && (a->data.num == (~b->data.num & irpn_True) )) {
974       return get_irn_n(a, 0);
975     }
976   }
977 #endif
978
979   /* If the Block has a Bad pred, we also have one. */
980   for (i = 0;  i < n_preds;  ++i)
981     if (is_Bad (get_Block_cfgpred(block, i)))
982       set_Phi_pred(n, i, new_Bad());
983
984   /* Find first non-self-referencing input */
985   for (i = 0;  i < n_preds;  ++i) {
986     first_val = get_Phi_pred(n, i);
987     if (   (first_val != n)                            /* not self pointer */
988 #if 1
989         && (get_irn_op(first_val) != op_Bad)
990 #endif
991            ) {        /* value not dead */
992       break;          /* then found first value. */
993     }
994   }
995
996   /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
997   if (i >= n_preds) { return new_Bad(); }
998
999   scnd_val = NULL;
1000
1001   /* follow_Id () for rest of inputs, determine if any of these
1002      are non-self-referencing */
1003   while (++i < n_preds) {
1004     scnd_val = get_Phi_pred(n, i);
1005     if (   (scnd_val != n)
1006         && (scnd_val != first_val)
1007 #if 1
1008         && (get_irn_op(scnd_val) != op_Bad)
1009 #endif
1010            ) {
1011       break;
1012     }
1013   }
1014
1015   /* Fold, if no multiple distinct non-self-referencing inputs */
1016   if (i >= n_preds) {
1017     n = first_val;
1018     DBG_OPT_PHI(oldn, first_val, n);
1019   } else {
1020     /* skip the remaining Ids (done in get_Phi_pred). */
1021     /* superfluous, since we walk all to propagate Block's Bads.
1022        while (++i < n_preds) get_Phi_pred(n, i);     */
1023   }
1024   return n;
1025 }
1026
1027 /**
1028  * optimize Proj(Tuple) and gigo for ProjX in Bad block
1029  */
1030 static ir_node *equivalent_node_Proj(ir_node *n)
1031 {
1032   ir_node *oldn = n;
1033
1034   ir_node *a = get_Proj_pred(n);
1035
1036   if ( get_irn_op(a) == op_Tuple) {
1037     /* Remove the Tuple/Proj combination. */
1038     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1039       n = get_Tuple_pred(a, get_Proj_proj(n));
1040       DBG_OPT_TUPLE(oldn, a, n);
1041     } else {
1042       assert(0); /* This should not happen! */
1043       n = new_Bad();
1044     }
1045   } else if (get_irn_mode(n) == mode_X &&
1046              is_Block_dead(get_nodes_block(n))) {
1047     /* Remove dead control flow -- early gigo. */
1048     n = new_Bad();
1049   }
1050   return n;
1051 }
1052
1053 /**
1054  * Remove Id's.
1055  */
1056 static ir_node *equivalent_node_Id(ir_node *n)
1057 {
1058   ir_node *oldn = n;
1059
1060   n = follow_Id(n);
1061   DBG_OPT_ID(oldn, n);
1062   return n;
1063 }
1064
1065 /**
1066  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1067  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1068  * new nodes.  It is therefore safe to free n if the node returned is not n.
1069  * If a node returns a Tuple we can not just skip it.  If the size of the
1070  * in array fits, we transform n into a tuple (e.g., Div).
1071  */
1072 ir_node *
1073 equivalent_node(ir_node *n)
1074 {
1075   if (n->op->equivalent_node)
1076     return n->op->equivalent_node(n);
1077   return n;
1078 }
1079
1080 /**
1081  * set the default equivalent node operation
1082  */
1083 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1084 {
1085 #define CASE(a)                                 \
1086   case iro_##a:                                 \
1087     op->equivalent_node  = equivalent_node_##a; \
1088     break
1089
1090   switch (op->code) {
1091   CASE(Block);
1092   CASE(Jmp);
1093   CASE(Cond);
1094   CASE(Or);
1095   CASE(Add);
1096   CASE(Eor);
1097   CASE(Sub);
1098   CASE(Shl);
1099   CASE(Shr);
1100   CASE(Shrs);
1101   CASE(Rot);
1102   CASE(Not);
1103   CASE(Minus);
1104   CASE(Mul);
1105   CASE(Div);
1106   CASE(DivMod);
1107   CASE(And);
1108   CASE(Conv);
1109   CASE(Cast);
1110   CASE(Phi);
1111   CASE(Proj);
1112   CASE(Id);
1113   default:
1114     op->equivalent_node  = NULL;
1115   }
1116
1117   return op;
1118 #undef CASE
1119 }
1120
1121 /**
1122  * Do node specific optimizations of nodes predecessors.
1123  */
1124 static void
1125 optimize_preds(ir_node *n) {
1126   ir_node *a = NULL, *b = NULL;
1127
1128   /* get the operands we will work on for simple cases. */
1129   if (is_binop(n)) {
1130     a = get_binop_left(n);
1131     b = get_binop_right(n);
1132   } else if (is_unop(n)) {
1133     a = get_unop_op(n);
1134   }
1135
1136   switch (get_irn_opcode(n)) {
1137
1138   case iro_Cmp:
1139     /* We don't want Cast as input to Cmp. */
1140     if (get_irn_op(a) == op_Cast) {
1141       a = get_Cast_op(a);
1142       set_Cmp_left(n, a);
1143     }
1144     if (get_irn_op(b) == op_Cast) {
1145       b = get_Cast_op(b);
1146       set_Cmp_right(n, b);
1147     }
1148     break;
1149
1150   default: break;
1151   } /* end switch */
1152 }
1153
1154 /**
1155  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1156  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)) if possible.
1157  */
1158 static ir_node *transform_node_AddSub(ir_node *n)
1159 {
1160   ir_mode *mode = get_irn_mode(n);
1161
1162   if (mode_is_reference(mode)) {
1163     ir_node *left  = get_binop_left(n);
1164     ir_node *right = get_binop_right(n);
1165     int ref_bits   = get_mode_size_bits(mode);
1166
1167     if (get_irn_op(left) == op_Conv) {
1168       ir_mode *mode = get_irn_mode(left);
1169       int bits      = get_mode_size_bits(mode);
1170
1171       if (ref_bits == bits &&
1172           mode_is_int(mode) &&
1173           get_mode_arithmetic(mode) == irma_twos_complement) {
1174         ir_node *pre      = get_Conv_op(left);
1175         ir_mode *pre_mode = get_irn_mode(pre);
1176
1177         if (mode_is_int(pre_mode) &&
1178             get_mode_size_bits(pre_mode) == bits &&
1179             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1180           /* ok, this conv just changes to sign, moreover the calculation
1181            * is done with same number of bits as our address mode, so
1182            * we can ignore the conv as address calculation can be viewed
1183            * as either signed or unsigned
1184            */
1185           set_binop_left(n, pre);
1186         }
1187       }
1188     }
1189
1190     if (get_irn_op(right) == op_Conv) {
1191       ir_mode *mode = get_irn_mode(right);
1192       int bits      = get_mode_size_bits(mode);
1193
1194       if (ref_bits == bits &&
1195           mode_is_int(mode) &&
1196           get_mode_arithmetic(mode) == irma_twos_complement) {
1197         ir_node *pre      = get_Conv_op(right);
1198         ir_mode *pre_mode = get_irn_mode(pre);
1199
1200         if (mode_is_int(pre_mode) &&
1201             get_mode_size_bits(pre_mode) == bits &&
1202             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1203           /* ok, this conv just changes to sign, moreover the calculation
1204            * is done with same number of bits as our address mode, so
1205            * we can ignore the conv as address calculation can be viewed
1206            * as either signed or unsigned
1207            */
1208           set_binop_right(n, pre);
1209         }
1210       }
1211     }
1212   }
1213   return n;
1214 }
1215
1216 #define transform_node_Add      transform_node_AddSub
1217 #define transform_node_Sub      transform_node_AddSub
1218
1219 /** Do architecture dependend optimizations on Mul nodes */
1220 static ir_node *transform_node_Mul(ir_node *n) {
1221   return arch_dep_replace_mul_with_shifts(n);
1222 }
1223
1224 static ir_node *transform_node_Div(ir_node *n)
1225 {
1226   tarval *tv = value_of(n);
1227   ir_node *value = n;
1228
1229   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1230
1231   if (tv != tarval_bad)
1232     value = new_Const(get_tarval_mode(tv), tv);
1233   else /* Try architecture dependand optimization */
1234     value = arch_dep_replace_div_by_const(n);
1235
1236   if (value != n) {
1237     /* Turn Div into a tuple (mem, bad, value) */
1238     ir_node *mem = get_Div_mem(n);
1239
1240     turn_into_tuple(n, 3);
1241     set_Tuple_pred(n, pn_Div_M, mem);
1242     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1243     set_Tuple_pred(n, pn_Div_res, value);
1244   }
1245   return n;
1246 }
1247
1248 static ir_node *transform_node_Mod(ir_node *n)
1249 {
1250   tarval *tv = value_of(n);
1251   ir_node *value = n;
1252
1253   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1254
1255   if (tv != tarval_bad)
1256     value = new_Const(get_tarval_mode(tv), tv);
1257   else /* Try architecture dependand optimization */
1258     value = arch_dep_replace_mod_by_const(n);
1259
1260   if (value != n) {
1261     /* Turn Mod into a tuple (mem, bad, value) */
1262     ir_node *mem = get_Mod_mem(n);
1263
1264     turn_into_tuple(n, 3);
1265     set_Tuple_pred(n, pn_Mod_M, mem);
1266     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1267     set_Tuple_pred(n, pn_Mod_res, value);
1268   }
1269   return n;
1270 }
1271
1272 static ir_node *transform_node_DivMod(ir_node *n)
1273 {
1274   int evaluated = 0;
1275
1276   ir_node *a = get_DivMod_left(n);
1277   ir_node *b = get_DivMod_right(n);
1278   ir_mode *mode = get_irn_mode(a);
1279   tarval *ta = value_of(a);
1280   tarval *tb = value_of(b);
1281
1282   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1283     return n;
1284
1285   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1286
1287   if (tb != tarval_bad) {
1288     if (tb == get_mode_one(get_tarval_mode(tb))) {
1289       b = new_Const (mode, get_mode_null(mode));
1290       evaluated = 1;
1291     } else if (ta != tarval_bad) {
1292       tarval *resa, *resb;
1293       resa = tarval_div (ta, tb);
1294       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1295                                         Jmp for X result!? */
1296       resb = tarval_mod (ta, tb);
1297       if (resb == tarval_bad) return n; /* Causes exception! */
1298       a = new_Const (mode, resa);
1299       b = new_Const (mode, resb);
1300       evaluated = 1;
1301     }
1302     else { /* Try architecture dependand optimization */
1303       arch_dep_replace_divmod_by_const(&a, &b, n);
1304       evaluated = a != NULL;
1305     }
1306   } else if (ta == get_mode_null(mode)) {
1307     /* 0 / non-Const = 0 */
1308     b = a;
1309     evaluated = 1;
1310   }
1311
1312   if (evaluated) { /* replace by tuple */
1313     ir_node *mem = get_DivMod_mem(n);
1314     turn_into_tuple(n, 4);
1315     set_Tuple_pred(n, pn_DivMod_M,        mem);
1316     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1317     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1318     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1319     assert(get_nodes_block(n));
1320   }
1321
1322   return n;
1323 }
1324
1325 static ir_node *transform_node_Cond(ir_node *n)
1326 {
1327   /* Replace the Cond by a Jmp if it branches on a constant
1328      condition. */
1329   ir_node *jmp;
1330   ir_node *a = get_Cond_selector(n);
1331   tarval *ta = value_of(a);
1332
1333   if ((ta != tarval_bad) &&
1334       (get_irn_mode(a) == mode_b) &&
1335       (get_opt_unreachable_code())) {
1336     /* It's a boolean Cond, branching on a boolean constant.
1337                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1338     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1339     turn_into_tuple(n, 2);
1340     if (ta == tarval_b_true) {
1341       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1342       set_Tuple_pred(n, pn_Cond_true, jmp);
1343     } else {
1344       set_Tuple_pred(n, pn_Cond_false, jmp);
1345       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1346     }
1347     /* We might generate an endless loop, so keep it alive. */
1348     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1349   } else if ((ta != tarval_bad) &&
1350              (get_irn_mode(a) == mode_Iu) &&
1351              (get_Cond_kind(n) == dense) &&
1352              (get_opt_unreachable_code())) {
1353     /* I don't want to allow Tuples smaller than the biggest Proj.
1354        Also this tuple might get really big...
1355        I generate the Jmp here, and remember it in link.  Link is used
1356        when optimizing Proj. */
1357     set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_block(n)));
1358     /* We might generate an endless loop, so keep it alive. */
1359     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1360   } else if ((get_irn_op(a) == op_Eor)
1361              && (get_irn_mode(a) == mode_b)
1362              && (classify_tarval(value_of(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1363     /* The Eor is a negate.  Generate a new Cond without the negate,
1364        simulate the negate by exchanging the results. */
1365     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1366                                get_Eor_left(a)));
1367   } else if ((get_irn_op(a) == op_Not)
1368              && (get_irn_mode(a) == mode_b)) {
1369     /* A Not before the Cond.  Generate a new Cond without the Not,
1370        simulate the Not by exchanging the results. */
1371     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1372                                get_Not_op(a)));
1373   }
1374   return n;
1375 }
1376
1377 /**
1378  * Transform an Eor.
1379  */
1380 static ir_node *transform_node_Eor(ir_node *n)
1381 {
1382   ir_node *a = get_Eor_left(n);
1383   ir_node *b = get_Eor_right(n);
1384
1385   if ((get_irn_mode(n) == mode_b)
1386       && (get_irn_op(a) == op_Proj)
1387       && (get_irn_mode(a) == mode_b)
1388       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1389       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1390     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1391     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1392                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1393   else if ((get_irn_mode(n) == mode_b)
1394            && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE))
1395     /* The Eor is a Not. Replace it by a Not. */
1396     /*   ????!!!Extend to bitfield 1111111. */
1397     n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode_b);
1398
1399   return n;
1400 }
1401
1402 /**
1403  * Transform a boolean Not.
1404  */
1405 static ir_node *transform_node_Not(ir_node *n)
1406 {
1407   ir_node *a = get_Not_op(n);
1408
1409   if (   (get_irn_mode(n) == mode_b)
1410       && (get_irn_op(a) == op_Proj)
1411       && (get_irn_mode(a) == mode_b)
1412       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1413     /* We negate a Cmp. The Cmp has the negated result anyways! */
1414     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1415                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1416
1417   return n;
1418 }
1419
1420 /**
1421  * Transform a Cast of a Const into a new Const
1422  */
1423 static ir_node *transform_node_Cast(ir_node *n) {
1424   ir_node *pred = get_Cast_op(n);
1425   type *tp = get_irn_type(pred);
1426
1427   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1428     n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
1429               get_Const_tarval(pred), tp);
1430   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1431     n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
1432                  get_SymConst_kind(pred), tp);
1433   }
1434   return n;
1435 }
1436
1437 /**
1438  * Transform a Div/Mod/DivMod with a non-zero constant. Must be
1439  * done here instead of equivalent node because it creates new
1440  * nodes.
1441  * Removes the exceptions and routes the memory to the NoMem node.
1442  *
1443  * Further, it optimizes jump tables by removing all impossible cases.
1444  */
1445 static ir_node *transform_node_Proj(ir_node *proj)
1446 {
1447   ir_node *n = get_Proj_pred(proj);
1448   ir_node *b;
1449   tarval *tb;
1450   long proj_nr;
1451
1452   switch (get_irn_opcode(n)) {
1453   case iro_Div:
1454     b  = get_Div_right(n);
1455     tb = value_of(b);
1456
1457     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* div(x, c) && c != 0 */
1458       proj_nr = get_Proj_proj(proj);
1459
1460       /* this node may float */
1461       set_irn_pinned(n, op_pin_state_floats);
1462
1463       if (proj_nr == pn_Div_X_except) {
1464         /* we found an exception handler, remove it */
1465         return new_Bad();
1466       } else {
1467         /* the memory Proj can be removed */
1468         ir_node *res = get_Div_mem(n);
1469 # ifdef USE_NOMEM
1470         set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1471 # endif /* defined USE_NOMEM */
1472         if (proj_nr == pn_Div_M)
1473           return res;
1474       }
1475     }
1476     break;
1477   case iro_Mod:
1478     b  = get_Mod_right(n);
1479     tb = value_of(b);
1480
1481     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* mod(x, c) && c != 0 */
1482       proj_nr = get_Proj_proj(proj);
1483
1484       /* this node may float */
1485       set_irn_pinned(n, op_pin_state_floats);
1486
1487       if (proj_nr == pn_Mod_X_except) {
1488         /* we found an exception handler, remove it */
1489         return new_Bad();
1490       } else {
1491         /* the memory Proj can be removed */
1492         ir_node *res = get_Mod_mem(n);
1493 # ifdef USE_NOMEM
1494         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1495 # endif /* defined USE_NOMEM */
1496         if (proj_nr == pn_Mod_M)
1497           return res;
1498       }
1499     }
1500     break;
1501   case iro_DivMod:
1502     b  = get_DivMod_right(n);
1503     tb = value_of(b);
1504
1505     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* DivMod(x, c) && c != 0 */
1506       proj_nr = get_Proj_proj(proj);
1507
1508       /* this node may float */
1509       set_irn_pinned(n, op_pin_state_floats);
1510
1511       if (proj_nr == pn_DivMod_X_except) {
1512         /* we found an exception handler, remove it */
1513         return new_Bad();
1514       }
1515       else {
1516         /* the memory Proj can be removed */
1517         ir_node *res = get_DivMod_mem(n);
1518 # ifdef USE_NOMEM
1519         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1520 # endif /* defined USE_NOMEM */
1521         if (proj_nr == pn_DivMod_M)
1522           return res;
1523       }
1524     }
1525     break;
1526
1527   case iro_Cond:
1528     if (get_opt_unreachable_code()) {
1529       b = get_Cond_selector(n);
1530       tb = value_of(b);
1531
1532       if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
1533         /* we have a constant switch */
1534         long num = get_Proj_proj(proj);
1535
1536         if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
1537           if (get_tarval_long(tb) == num) {
1538             /* Do NOT create a jump here, or we will have 2 control flow ops
1539              * in a block. This case is optimized away in optimize_cf(). */
1540             return proj;
1541           }
1542           else
1543             return new_Bad();
1544         }
1545       }
1546     }
1547     return proj;
1548
1549   case iro_Tuple:
1550     /* should not happen, but if it does will be optimized away */
1551     break;
1552
1553   default:
1554     /* do nothing */
1555     return proj;
1556   }
1557
1558   /* we have added a Tuple, optimize it for the current Proj away */
1559   return equivalent_node_Proj(proj);
1560 }
1561
1562 /**
1563  * returns the operands of a commutative bin-op, if one operand is
1564  * a const, it is returned as the second one.
1565  */
1566 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
1567 {
1568   ir_node *op_a = get_binop_left(binop);
1569   ir_node *op_b = get_binop_right(binop);
1570
1571   assert(is_op_commutative(get_irn_op(binop)));
1572
1573   if (get_irn_op(op_a) == op_Const) {
1574     *a = op_b;
1575     *c = op_a;
1576   }
1577   else {
1578     *a = op_a;
1579     *c = op_b;
1580   }
1581 }
1582
1583 /**
1584  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1585  * Such pattern may arise in bitfield stores.
1586  *
1587  * value  c4                  value      c4 & c2
1588  *    AND     c3                    AND           c1 | c3
1589  *        OR     c2      ===>               OR
1590  *           AND    c1
1591  *               OR
1592  */
1593 static ir_node *transform_node_Or(ir_node *or)
1594 {
1595   ir_node *and, *c1;
1596   ir_node *or_l, *c2;
1597   ir_node *and_l, *c3;
1598   ir_node *value, *c4;
1599   ir_node *new_and, *new_const, *block;
1600   ir_mode *mode = get_irn_mode(or);
1601
1602   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
1603
1604   get_comm_Binop_Ops(or, &and, &c1);
1605   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
1606     return or;
1607
1608   get_comm_Binop_Ops(and, &or_l, &c2);
1609   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
1610     return or;
1611
1612   get_comm_Binop_Ops(or_l, &and_l, &c3);
1613   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
1614     return or;
1615
1616   get_comm_Binop_Ops(and_l, &value, &c4);
1617   if (get_irn_op(c4) != op_Const)
1618     return or;
1619
1620   /* ok, found the pattern, check for conditions */
1621   assert(mode == get_irn_mode(and));
1622   assert(mode == get_irn_mode(or_l));
1623   assert(mode == get_irn_mode(and_l));
1624
1625   tv1 = get_Const_tarval(c1);
1626   tv2 = get_Const_tarval(c2);
1627   tv3 = get_Const_tarval(c3);
1628   tv4 = get_Const_tarval(c4);
1629
1630   tv = tarval_or(tv4, tv2);
1631   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
1632     /* have at least one 0 at the same bit position */
1633     return or;
1634   }
1635
1636   n_tv4 = tarval_not(tv4);
1637   if (tv3 != tarval_and(tv3, n_tv4)) {
1638     /* bit in the or_mask is outside the and_mask */
1639     return or;
1640   }
1641
1642   n_tv2 = tarval_not(tv2);
1643   if (tv1 != tarval_and(tv1, n_tv2)) {
1644     /* bit in the or_mask is outside the and_mask */
1645     return or;
1646   }
1647
1648   /* ok, all conditions met */
1649   block = get_nodes_block(or);
1650
1651   new_and = new_r_And(current_ir_graph, block,
1652       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
1653
1654   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
1655
1656   set_Or_left(or, new_and);
1657   set_Or_right(or, new_const);
1658
1659   /* check for more */
1660   return transform_node_Or(or);
1661 }
1662
1663 /* forward */
1664 static ir_node *transform_node(ir_node *n);
1665
1666 /**
1667  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
1668  */
1669 static ir_node * transform_node_shift(ir_node *n)
1670 {
1671   ir_node *left;
1672   tarval *tv1, *tv2, *res;
1673   ir_mode *mode;
1674   int modulo_shf, flag;
1675
1676   left = get_binop_left(n);
1677
1678   /* different operations */
1679   if (get_irn_op(left) != get_irn_op(n))
1680     return n;
1681
1682   tv1 = value_of(get_binop_right(n));
1683   if (tv1 == tarval_bad)
1684     return n;
1685
1686   tv2 = value_of(get_binop_right(left));
1687   if (tv2 == tarval_bad)
1688     return n;
1689
1690   res = tarval_add(tv1, tv2);
1691
1692   /* beware: a simple replacement works only, if res < modulo shift */
1693   mode = get_irn_mode(n);
1694
1695   flag = 0;
1696
1697   modulo_shf = get_mode_modulo_shift(mode);
1698   if (modulo_shf > 0) {
1699     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
1700
1701     if (tarval_cmp(res, modulo) & Lt)
1702       flag = 1;
1703   }
1704   else
1705     flag = 1;
1706
1707   if (flag) {
1708     /* ok, we can replace it */
1709     ir_node *in[2], *irn, *block = get_nodes_block(n);
1710
1711     in[0] = get_binop_left(left);
1712     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
1713
1714     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
1715
1716     return transform_node(irn);
1717   }
1718   return n;
1719 }
1720
1721
1722 /**
1723  * Tries several [inplace] [optimizing] transformations and returns an
1724  * equivalent node.  The difference to equivalent_node() is that these
1725  * transformations _do_ generate new nodes, and thus the old node must
1726  * not be freed even if the equivalent node isn't the old one.
1727  */
1728 static ir_node *transform_node(ir_node *n)
1729 {
1730   if (n->op->transform_node)
1731     n = n->op->transform_node(n);
1732   return n;
1733 }
1734
1735 /**
1736  * set the default transform node operation
1737  */
1738 static ir_op *firm_set_default_transform_node(ir_op *op)
1739 {
1740 #define CASE(a)                                 \
1741   case iro_##a:                                 \
1742     op->transform_node  = transform_node_##a;   \
1743     break
1744
1745   switch (op->code) {
1746   CASE(Add);
1747   CASE(Sub);
1748   CASE(Mul);
1749   CASE(Div);
1750   CASE(Mod);
1751   CASE(DivMod);
1752   CASE(Cond);
1753   CASE(Eor);
1754   CASE(Not);
1755   CASE(Cast);
1756   CASE(Proj);
1757   CASE(Or);
1758   case iro_Shr:
1759   case iro_Shrs:
1760   case iro_Shl:
1761     op->transform_node  = transform_node_shift;
1762     break;
1763   default:
1764     op->transform_node  = NULL;
1765   }
1766
1767   return op;
1768 #undef CASE
1769 }
1770
1771
1772 /* **************** Common Subexpression Elimination **************** */
1773
1774 /** The size of the hash table used, should estimate the number of nodes
1775     in a graph. */
1776 #define N_IR_NODES 512
1777
1778 /** Compares the attributes of two Const nodes. */
1779 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
1780 {
1781   return (get_Const_tarval(a) != get_Const_tarval(b))
1782       || (get_Const_type(a) != get_Const_type(b));
1783 }
1784
1785 /** Compares the attributes of two Proj nodes. */
1786 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
1787 {
1788     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
1789 }
1790
1791 /** Compares the attributes of two Filter nodes. */
1792 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
1793 {
1794     return get_Filter_proj(a) != get_Filter_proj(b);
1795 }
1796
1797 /** Compares the attributes of two Alloc nodes. */
1798 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
1799 {
1800     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
1801         || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
1802 }
1803
1804 /** Compares the attributes of two Free nodes. */
1805 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
1806 {
1807     return (get_irn_free_attr(a) != get_irn_free_attr(b));
1808 }
1809
1810 /** Compares the attributes of two SymConst nodes. */
1811 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
1812 {
1813     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
1814       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
1815       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
1816 }
1817
1818 /** Compares the attributes of two Call nodes. */
1819 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
1820 {
1821     return (get_irn_call_attr(a) != get_irn_call_attr(b));
1822 }
1823
1824 /** Compares the attributes of two Sel nodes. */
1825 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
1826 {
1827     return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
1828       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
1829       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
1830       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
1831       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
1832 }
1833
1834 /** Compares the attributes of two Phi nodes. */
1835 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
1836 {
1837     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
1838 }
1839
1840 /** Compares the attributes of two Cast nodes. */
1841 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
1842 {
1843     return get_Cast_type(a) != get_Cast_type(b);
1844 }
1845
1846 /** Compares the attributes of two Load nodes. */
1847 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
1848 {
1849   if (get_Load_volatility(a) == volatility_is_volatile ||
1850       get_Load_volatility(b) == volatility_is_volatile)
1851     /* NEVER do CSE on volatile Loads */
1852     return 1;
1853
1854   return get_Load_mode(a) != get_Load_mode(b);
1855 }
1856
1857 /** Compares the attributes of two Store nodes. */
1858 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
1859 {
1860   /* NEVER do CSE on volatile Stores */
1861   return (get_Store_volatility(a) == volatility_is_volatile ||
1862       get_Store_volatility(b) == volatility_is_volatile);
1863 }
1864
1865 /**
1866  * set the default node attribute compare operation
1867  */
1868 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
1869 {
1870 #define CASE(a)                             \
1871   case iro_##a:                             \
1872     op->node_cmp_attr  = node_cmp_attr_##a; \
1873     break
1874
1875   switch (op->code) {
1876   CASE(Const);
1877   CASE(Proj);
1878   CASE(Filter);
1879   CASE(Alloc);
1880   CASE(Free);
1881   CASE(SymConst);
1882   CASE(Call);
1883   CASE(Sel);
1884   CASE(Phi);
1885   CASE(Cast);
1886   CASE(Load);
1887   CASE(Store);
1888   default:
1889     op->node_cmp_attr  = NULL;
1890   }
1891
1892   return op;
1893 #undef CASE
1894 }
1895
1896 /**
1897  * Compare function for two nodes in the hash table. Gets two
1898  * nodes as parameters.  Returns 0 if the nodes are a cse.
1899  */
1900 static int
1901 vt_cmp (const void *elt, const void *key)
1902 {
1903   ir_node *a, *b;
1904   int i, irn_arity_a;
1905
1906   a = (void *)elt;
1907   b = (void *)key;
1908
1909   if (a == b) return 0;
1910
1911   if ((get_irn_op(a) != get_irn_op(b)) ||
1912       (get_irn_mode(a) != get_irn_mode(b))) return 1;
1913
1914   /* compare if a's in and b's in are of equal length */
1915   irn_arity_a = get_irn_intra_arity (a);
1916   if (irn_arity_a != get_irn_intra_arity(b))
1917     return 1;
1918
1919   /* for block-local cse and op_pin_state_pinned nodes: */
1920   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
1921     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
1922       return 1;
1923   }
1924
1925   /* compare a->in[0..ins] with b->in[0..ins] */
1926   for (i = 0; i < irn_arity_a; i++)
1927     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
1928       return 1;
1929
1930   /*
1931    * here, we already now that the nodes are identical except their
1932    * attributes
1933    */
1934   if (a->op->node_cmp_attr)
1935     return a->op->node_cmp_attr(a, b);
1936
1937   return 0;
1938 }
1939
1940 /*
1941  * Calculate a hash value of a node.
1942  */
1943 unsigned
1944 ir_node_hash (ir_node *node)
1945 {
1946   unsigned h;
1947   int i, irn_arity;
1948
1949   if (node->op == op_Const) {
1950     /* special value for const, as they only differ in their tarval. */
1951     h = HASH_PTR(node->attr.con.tv);
1952     h = 9*h + HASH_PTR(get_irn_mode(node));
1953   } else if (node->op == op_SymConst) {
1954     /* special value for const, as they only differ in their symbol. */
1955     h = HASH_PTR(node->attr.i.sym.type_p);
1956     h = 9*h + HASH_PTR(get_irn_mode(node));
1957   } else {
1958
1959     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
1960     h = irn_arity = get_irn_intra_arity(node);
1961
1962     /* consider all in nodes... except the block if not a control flow. */
1963     for (i =  is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
1964       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
1965     }
1966
1967     /* ...mode,... */
1968     h = 9*h + HASH_PTR(get_irn_mode(node));
1969     /* ...and code */
1970     h = 9*h + HASH_PTR(get_irn_op(node));
1971   }
1972
1973   return h;
1974 }
1975
1976 pset *
1977 new_identities(void) {
1978   return new_pset(vt_cmp, N_IR_NODES);
1979 }
1980
1981 void
1982 del_identities(pset *value_table) {
1983   del_pset(value_table);
1984 }
1985
1986 /**
1987  * Return the canonical node computing the same value as n.
1988  * Looks up the node in a hash table.
1989  *
1990  * For Const nodes this is performed in the constructor, too.  Const
1991  * nodes are extremely time critical because of their frequent use in
1992  * constant string arrays.
1993  */
1994 static INLINE ir_node *
1995 identify (pset *value_table, ir_node *n)
1996 {
1997   ir_node *o = NULL;
1998
1999   if (!value_table) return n;
2000
2001   if (get_opt_reassociation()) {
2002     if (is_op_commutative(get_irn_op(n))) {
2003       ir_node *l = get_binop_left(n);
2004       ir_node *r = get_binop_right(n);
2005
2006       /* for commutative operators perform  a OP b == b OP a */
2007       if (l > r) {
2008         set_binop_left(n, r);
2009         set_binop_right(n, l);
2010       }
2011     }
2012   }
2013
2014   o = pset_find (value_table, n, ir_node_hash (n));
2015   if (!o) return n;
2016
2017   DBG_OPT_CSE(n, o);
2018
2019   return o;
2020 }
2021
2022 /**
2023  * During construction we set the op_pin_state_pinned flag in the graph right when the
2024  * optimization is performed.  The flag turning on procedure global cse could
2025  * be changed between two allocations.  This way we are safe.
2026  */
2027 static INLINE ir_node *
2028 identify_cons (pset *value_table, ir_node *n) {
2029   ir_node *old = n;
2030
2031   n = identify(value_table, n);
2032   if (get_irn_n(old, -1) != get_irn_n(n, -1))
2033     set_irg_pinned(current_ir_graph, op_pin_state_floats);
2034   return n;
2035 }
2036
2037 /**
2038  * Return the canonical node computing the same value as n.
2039  * Looks up the node in a hash table, enters it in the table
2040  * if it isn't there yet.
2041  */
2042 static ir_node *
2043 identify_remember (pset *value_table, ir_node *n)
2044 {
2045   ir_node *o = NULL;
2046
2047   if (!value_table) return n;
2048
2049   if (get_opt_reassociation()) {
2050     if (is_op_commutative(get_irn_op(n))) {
2051       ir_node *l = get_binop_left(n);
2052       ir_node *r = get_binop_right(n);
2053
2054       /* for commutative operators perform  a OP b == b OP a */
2055       if (l > r) {
2056         set_binop_left(n, r);
2057         set_binop_right(n, l);
2058       }
2059     }
2060   }
2061
2062   /* lookup or insert in hash table with given hash key. */
2063   o = pset_insert (value_table, n, ir_node_hash (n));
2064
2065   if (o != n) {
2066     DBG_OPT_CSE(n, o);
2067   }
2068
2069   return o;
2070 }
2071
2072 void
2073 add_identities (pset *value_table, ir_node *node) {
2074   if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
2075     identify_remember (value_table, node);
2076 }
2077
2078 /**
2079  * garbage in, garbage out. If a node has a dead input, i.e., the
2080  * Bad node is input to the node, return the Bad node.
2081  */
2082 static INLINE ir_node *
2083 gigo (ir_node *node)
2084 {
2085   int i, irn_arity;
2086   ir_op* op = get_irn_op(node);
2087
2088   /* remove garbage blocks by looking at control flow that leaves the block
2089      and replacing the control flow by Bad. */
2090   if (get_irn_mode(node) == mode_X) {
2091     ir_node *block = get_nodes_block(node);
2092     if (op == op_End) return node;     /* Don't optimize End, may have Bads. */
2093
2094     if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
2095       irn_arity = get_irn_arity(block);
2096       for (i = 0; i < irn_arity; i++) {
2097         if (!is_Bad(get_irn_n(block, i))) break;
2098       }
2099       if (i == irn_arity) return new_Bad();
2100     }
2101   }
2102
2103   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
2104      blocks predecessors is dead. */
2105   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
2106     irn_arity = get_irn_arity(node);
2107
2108     if (is_Block_dead(get_nodes_block(node)))
2109       return new_Bad();
2110
2111     for (i = 0; i < irn_arity; i++) {
2112       if (is_Bad(get_irn_n(node, i))) {
2113         return new_Bad();
2114       }
2115     }
2116   }
2117 #if 0
2118   /* With this code we violate the agreement that local_optimize
2119      only leaves Bads in Block, Phi and Tuple nodes. */
2120   /* If Block has only Bads as predecessors it's garbage. */
2121   /* If Phi has only Bads as predecessors it's garbage. */
2122   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
2123     irn_arity = get_irn_arity(node);
2124     for (i = 0; i < irn_arity; i++) {
2125       if (!is_Bad(get_irn_n(node, i))) break;
2126     }
2127     if (i == irn_arity) node = new_Bad();
2128   }
2129 #endif
2130   return node;
2131 }
2132
2133
2134 /**
2135  * These optimizations deallocate nodes from the obstack.
2136  * It can only be called if it is guaranteed that no other nodes
2137  * reference this one, i.e., right after construction of a node.
2138  */
2139 ir_node *
2140 optimize_node (ir_node *n)
2141 {
2142   tarval *tv;
2143   ir_node *oldn = n;
2144   opcode iro = get_irn_opcode(n);
2145
2146   type *old_tp = get_irn_type(n);
2147   {
2148     int i, arity = get_irn_arity(n);
2149     for (i = 0; i < arity && !old_tp; ++i)
2150       old_tp = get_irn_type(get_irn_n(n, i));
2151   }
2152
2153   /* Allways optimize Phi nodes: part of the construction. */
2154   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
2155
2156   /* constant expression evaluation / constant folding */
2157   if (get_opt_constant_folding()) {
2158     /* constants can not be evaluated */
2159     if (iro != iro_Const) {
2160       /* try to evaluate */
2161       tv = computed_value(n);
2162       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
2163         /*
2164          * we MUST copy the node here temporary, because it's still needed
2165          * for DBG_OPT_CSTEVAL
2166          */
2167         int node_size = offsetof(ir_node, attr) +  n->op->attr_size;
2168         oldn = alloca(node_size);
2169
2170         memcpy(oldn, n, node_size);
2171     CLONE_ARR_A(ir_node *, oldn->in, n->in);
2172
2173     /* ARG, copy the in array, we need it for statistics */
2174     memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
2175
2176         /* evaluation was successful -- replace the node. */
2177         obstack_free (current_ir_graph->obst, n);
2178         n = new_Const (get_tarval_mode (tv), tv);
2179     if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
2180       set_Const_type(n, old_tp);
2181                                                  DBG_OPT_CSTEVAL(oldn, n);
2182         return n;
2183       }
2184     }
2185   }
2186
2187   /* remove unnecessary nodes */
2188   if (get_opt_constant_folding() ||
2189       (iro == iro_Phi)  ||   /* always optimize these nodes. */
2190       (iro == iro_Id)   ||
2191       (iro == iro_Proj) ||
2192       (iro == iro_Block)  )  /* Flags tested local. */
2193     n = equivalent_node (n);
2194
2195   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
2196
2197   /** common subexpression elimination **/
2198   /* Checks whether n is already available. */
2199   /* The block input is used to distinguish different subexpressions. Right
2200      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
2201      subexpressions within a block. */
2202   if (get_opt_cse())
2203     n = identify_cons (current_ir_graph->value_table, n);
2204
2205   if (n != oldn) {
2206     /* We found an existing, better node, so we can deallocate the old node. */
2207     obstack_free (current_ir_graph->obst, oldn);
2208
2209     return n;
2210   }
2211
2212   /* Some more constant expression evaluation that does not allow to
2213      free the node. */
2214   iro = get_irn_opcode(n);
2215   if (get_opt_constant_folding() ||
2216       (iro == iro_Cond) ||
2217       (iro == iro_Proj))     /* Flags tested local. */
2218     n = transform_node (n);
2219
2220   /* Remove nodes with dead (Bad) input.
2221      Run always for transformation induced Bads. */
2222   n = gigo (n);
2223
2224   /* Now we have a legal, useful node. Enter it in hash table for cse */
2225   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
2226     n = identify_remember (current_ir_graph->value_table, n);
2227   }
2228
2229   return n;
2230 }
2231
2232
2233 /**
2234  * These optimizations never deallocate nodes.  This can cause dead
2235  * nodes lying on the obstack.  Remove these by a dead node elimination,
2236  * i.e., a copying garbage collection.
2237  */
2238 ir_node *
2239 optimize_in_place_2 (ir_node *n)
2240 {
2241   tarval *tv;
2242   ir_node *oldn = n;
2243   opcode iro = get_irn_opcode(n);
2244
2245   type *old_tp = get_irn_type(n);
2246   {
2247     int i, arity = get_irn_arity(n);
2248     for (i = 0; i < arity && !old_tp; ++i)
2249       old_tp = get_irn_type(get_irn_n(n, i));
2250   }
2251
2252   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
2253
2254   /* if not optimize return n */
2255   if (n == NULL) {
2256     assert(0);
2257     /* Here this is possible.  Why? */
2258     return n;
2259   }
2260
2261   /* constant expression evaluation / constant folding */
2262   if (get_opt_constant_folding()) {
2263     /* constants can not be evaluated */
2264     if (iro != iro_Const) {
2265       /* try to evaluate */
2266       tv = computed_value(n);
2267       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
2268         /* evaluation was successful -- replace the node. */
2269         n = new_Const (get_tarval_mode (tv), tv);
2270
2271     if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
2272       set_Const_type(n, old_tp);
2273
2274         DBG_OPT_CSTEVAL(oldn, n);
2275         return n;
2276       }
2277     }
2278   }
2279
2280   /* remove unnecessary nodes */
2281   if (get_opt_constant_folding() ||
2282       (iro == iro_Phi)  ||   /* always optimize these nodes. */
2283       (iro == iro_Id)   ||   /* ... */
2284       (iro == iro_Proj) ||   /* ... */
2285       (iro == iro_Block)  )  /* Flags tested local. */
2286     n = equivalent_node (n);
2287
2288   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
2289
2290   /** common subexpression elimination **/
2291   /* Checks whether n is already available. */
2292   /* The block input is used to distinguish different subexpressions.  Right
2293      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
2294      subexpressions within a block. */
2295   if (get_opt_cse()) {
2296     n = identify (current_ir_graph->value_table, n);
2297   }
2298
2299   /* Some more constant expression evaluation. */
2300   iro = get_irn_opcode(n);
2301   if (get_opt_constant_folding() ||
2302       (iro == iro_Cond) ||
2303       (iro == iro_Proj))     /* Flags tested local. */
2304     n = transform_node (n);
2305
2306   /* Remove nodes with dead (Bad) input.
2307      Run always for transformation induced Bads.  */
2308   n = gigo (n);
2309
2310   /* Now we can verify the node, as it has no dead inputs any more. */
2311   irn_vrfy(n);
2312
2313   /* Now we have a legal, useful node. Enter it in hash table for cse.
2314      Blocks should be unique anyways.  (Except the successor of start:
2315      is cse with the start block!) */
2316   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
2317     n = identify_remember (current_ir_graph->value_table, n);
2318
2319   return n;
2320 }
2321
2322 /**
2323  * Wrapper for external use, set proper status bits after optimization.
2324  */
2325 ir_node *
2326 optimize_in_place (ir_node *n)
2327 {
2328   /* Handle graph state */
2329   assert(get_irg_phase_state(current_ir_graph) != phase_building);
2330
2331   if (get_opt_global_cse())
2332     set_irg_pinned(current_ir_graph, op_pin_state_floats);
2333   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
2334     set_irg_outs_inconsistent(current_ir_graph);
2335
2336   /* Maybe we could also test whether optimizing the node can
2337      change the control graph. */
2338   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
2339     set_irg_dom_inconsistent(current_ir_graph);
2340   return optimize_in_place_2 (n);
2341 }
2342
2343 /**
2344  * set the default ir op operations
2345  */
2346 ir_op *firm_set_default_operations(ir_op *op)
2347 {
2348   op = firm_set_default_computed_value(op);
2349   op = firm_set_default_equivalent_node(op);
2350   op = firm_set_default_transform_node(op);
2351   op = firm_set_default_node_cmp_attr(op);
2352
2353   return op;
2354 }