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