Normalisierung der Zugriffsfunktionen,
[libfirm] / ir / ir / iropt.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** iropt --- optimizations intertwined with IR construction.
7 */
8
9 /* $Id$ */
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 # include "irnode_t.h"
16 # include "irgraph_t.h"
17 # include "iropt_t.h"
18 # include "ircons.h"
19 # include "irgmod.h"
20 # include "irvrfy.h"
21 # include "tv.h"
22 # include "tune.h"
23 # include "dbginfo_t.h"
24 # include "iropt_dbg.c"
25
26 /* Make types visible to allow most efficient access */
27 # include "entity_t.h"
28
29 /* Trivial INLINEable routine for copy propagation.
30    Does follow Ids, needed to optimize INLINEd code. */
31 static INLINE ir_node *
32 follow_Id (ir_node *n)
33 {
34   while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
35   return n;
36 }
37
38 static INLINE tarval *
39 value_of (ir_node *n)
40 {
41   if ((n != NULL) && (get_irn_op(n) == op_Const))
42     return get_Const_tarval(n);
43   else
44     return NULL;
45 }
46
47 /* if n can be computed, return the value, else NULL. Performs
48    constant folding. GL: Only if n is arithmetic operator? */
49 tarval *
50 computed_value (ir_node *n)
51 {
52   tarval *res;
53
54   ir_node *a = NULL, *b = NULL;  /* initialized to shut up gcc */
55   tarval *ta = NULL, *tb = NULL; /* initialized to shut up gcc */
56
57   res = NULL;
58
59   /* get the operands we will work on for simple cases. */
60   if (is_binop(n)) {
61     a = get_binop_left(n);
62     b = get_binop_right(n);
63   } else if (is_unop(n)) {
64     a = get_unop_op(n);
65   }
66
67   /* if the operands are constants, get the target value, else set it NULL.
68      (a and b may be NULL if we treat a node that is no computation.) */
69   ta = value_of (a);
70   tb = value_of (b);
71
72   /* Perform the constant evaluation / computation. */
73   switch (get_irn_opcode(n)) {
74   case iro_Const:
75     res = get_Const_tarval(n);
76     break;
77   case iro_SymConst:
78     if ((get_SymConst_kind(n) == size) &&
79         (get_type_state(get_SymConst_type(n))) == layout_fixed)
80       res = tarval_from_long (mode_i, get_type_size(get_SymConst_type(n)));
81     break;
82   case iro_Add:
83     if (ta && tb && (get_irn_mode(a) == get_irn_mode(b))
84         && (get_irn_mode(a) != mode_p)) {
85       res = tarval_add (ta, tb);
86     }
87     break;
88   case iro_Sub:
89     if (ta && tb && (get_irn_mode(a) == get_irn_mode(b))
90         && (get_irn_mode(a) != mode_p)) {
91       res = tarval_sub (ta, tb);
92     } else if (a == b) {
93       res = tarval_mode_null [get_irn_modecode (n)];
94     }
95     break;
96   case iro_Minus:
97       if (ta && mode_is_float(get_irn_mode(a)))
98         res = tarval_neg (ta);
99     break;
100   case iro_Mul:
101     if (ta && tb) /* tarval_mul tests for equivalent modes itself */ {
102       res = tarval_mul (ta, tb);
103     } else {
104       /* a*0 = 0 or 0*b = 0:
105          calls computed_value recursive and returns the 0 with proper
106          mode. */
107       tarval *v;
108       if (   (tarval_classify ((v = computed_value (a))) == 0)
109           || (tarval_classify ((v = computed_value (b))) == 0)) {
110         res = v;
111       }
112     }
113     break;
114   case iro_Quot:
115     /* This was missing in original implementation. Why? */
116     if (ta && tb  && (get_irn_mode(a) == get_irn_mode(b))) {
117       if (tarval_classify(tb) == 0) {res = NULL; break;}
118       res = tarval_quo(ta, tb);
119     }
120     break;
121   case iro_Div:
122     /* This was missing in original implementation. Why? */
123     if (ta && tb  && (get_irn_mode(a) == get_irn_mode(b))) {
124       if (tarval_classify(tb) == 0) {res = NULL; break;}
125       res = tarval_div(ta, tb);
126     }
127     break;
128   case iro_Mod:
129     /* This was missing in original implementation. Why? */
130     if (ta && tb  && (get_irn_mode(a) == get_irn_mode(b))) {
131       if (tarval_classify(tb) == 0) {res = NULL; break;}
132       res = tarval_mod(ta, tb);
133     }
134     break;
135   /* for iro_DivMod see iro_Proj */
136   case iro_Abs:
137     if (ta)
138       res = tarval_abs (ta);
139     break;
140   case iro_And:
141     if (ta && tb) {
142       res = tarval_and (ta, tb);
143     } else {
144       tarval *v;
145       if (   (tarval_classify ((v = computed_value (a))) == 0)
146           || (tarval_classify ((v = computed_value (b))) == 0)) {
147         res = v;
148       }
149     }
150     break;
151   case iro_Or:
152     if (ta && tb) {
153       res = tarval_or (ta, tb);
154     } else {
155       tarval *v;
156       if (   (tarval_classify ((v = computed_value (a))) == -1)
157           || (tarval_classify ((v = computed_value (b))) == -1)) {
158         res = v;
159       }
160     }
161     break;
162   case iro_Eor: if (ta && tb) { res = tarval_eor (ta, tb); } break;
163   case iro_Not: if (ta)       { res = tarval_neg (ta); } break;
164   case iro_Shl: if (ta && tb) { res = tarval_shl (ta, tb); } break;
165     /* tarval_shr is faulty !! */
166   case iro_Shr: if (ta && tb) { res = tarval_shr (ta, tb); } break;
167   case iro_Shrs:if (ta && tb) { /*res = tarval_shrs (ta, tb)*/; } break;
168   case iro_Rot: if (ta && tb) { /*res = tarval_rot (ta, tb)*/; } break;
169   case iro_Conv:if (ta)       { res = tarval_convert_to (ta, get_irn_mode (n)); }
170     break;
171   case iro_Proj:  /* iro_Cmp */
172     {
173       ir_node *aa, *ab;
174
175       a = get_Proj_pred(n);
176       /* Optimize Cmp nodes.
177          This performs a first step of unreachable code elimination.
178          Proj can not be computed, but folding a Cmp above the Proj here is
179          not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
180          only 1 is used.
181          There are several case where we can evaluate a Cmp node:
182          1. The nodes compared are both the same.  If we compare for
183             equal, this will return true, else it will return false.
184             This step relies on cse.
185          2. The predecessors of Cmp are target values.  We can evaluate
186             the Cmp.
187          3. The predecessors are Allocs or void* constants.  Allocs never
188             return NULL, they raise an exception.   Therefore we can predict
189             the Cmp result. */
190       if (get_irn_op(a) == op_Cmp) {
191         aa = get_Cmp_left(a);
192         ab = get_Cmp_right(a);
193         if (aa == ab) { /* 1.: */
194           /* This is a tric with the bits used for encoding the Cmp
195              Proj numbers, the following statement is not the same:
196           res = tarval_from_long (mode_b, (get_Proj_proj(n) == Eq)):    */
197           res = tarval_from_long (mode_b, (get_Proj_proj(n) & irpn_Eq));
198         } else {
199           tarval *taa = computed_value (aa);
200           tarval *tab = computed_value (ab);
201           if (taa && tab) { /* 2.: */
202             /* strange checks... */
203             ir_pncmp flags = tarval_comp (taa, tab);
204             if (flags != irpn_False) {
205               res = tarval_from_long (mode_b, get_Proj_proj(n) & flags);
206             }
207           } else {  /* check for 3.: */
208             ir_node *aaa = skip_nop(skip_Proj(aa));
209             ir_node *aba = skip_nop(skip_Proj(ab));
210             if (   (   (/* aa is ProjP and aaa is Alloc */
211                            (get_irn_op(aa) == op_Proj)
212                         && (get_irn_mode(aa) == mode_p)
213                         && (get_irn_op(aaa) == op_Alloc))
214                     && (   (/* ab is constant void */
215                                (get_irn_op(ab) == op_Const)
216                             && (get_irn_mode(ab) == mode_p)
217                             && (get_Const_tarval(ab) == tarval_p_void))
218                         || (/* ab is other Alloc */
219                                (get_irn_op(ab) == op_Proj)
220                             && (get_irn_mode(ab) == mode_p)
221                             && (get_irn_op(aba) == op_Alloc)
222                             && (aaa != aba))))
223                 || (/* aa is void and aba is Alloc */
224                        (get_irn_op(aa) == op_Const)
225                     && (get_irn_mode(aa) == mode_p)
226                     && (get_Const_tarval(aa) == tarval_p_void)
227                     && (get_irn_op(ab) == op_Proj)
228                     && (get_irn_mode(ab) == mode_p)
229                     && (get_irn_op(aba) == op_Alloc)))
230               /* 3.: */
231               res = tarval_from_long (mode_b, get_Proj_proj(n) & irpn_Ne);
232           }
233         }
234       } else if (get_irn_op(a) == op_DivMod) {
235         ta = value_of(get_DivMod_left(a));
236         tb = value_of(get_DivMod_right(a));
237         if (ta && tb  && (get_irn_mode(a) == get_irn_mode(b))) {
238           if (tarval_classify(tb) == 0) {res = NULL; break;}
239           if (get_Proj_proj(n)== 0) /* Div */
240             res = tarval_div(ta, tb);
241           else /* Mod */
242             res = tarval_mod(ta, tb);
243         }
244       }
245     }
246     break;
247   default:  ;
248   }
249
250   return res;
251 }  /* compute node */
252
253
254
255 /* returns 1 if the a and b are pointers to different locations. */
256 bool
257 different_identity (ir_node *a, ir_node *b)
258 {
259   assert (get_irn_mode (a) == mode_p
260           && get_irn_mode (b) == mode_p);
261
262   if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
263     ir_node *a1 = get_Proj_pred (a);
264     ir_node *b1 = get_Proj_pred (b);
265     if (a1 != b1 && get_irn_op (a1) == op_Alloc
266                 && get_irn_op (b1) == op_Alloc)
267       return 1;
268   }
269   return 0;
270 }
271
272
273 /* equivalent_node returns a node equivalent to N.  It skips all nodes that
274    perform no actual computation, as, e.g., the Id nodes.  It does not create
275    new nodes.  It is therefore safe to free N if the node returned is not N.
276    If a node returns a Tuple we can not just skip it.  If the size of the
277    in array fits, we transform n into a tuple (e.g., Div). */
278 ir_node *
279 equivalent_node (ir_node *n)
280 {
281   int ins;
282   ir_node *a = NULL; /* to shutup gcc */
283   ir_node *b = NULL; /* to shutup gcc */
284   ir_node *c = NULL; /* to shutup gcc */
285   ir_node *oldn = n;
286
287   ins = get_irn_arity (n);
288
289   /* get the operands we will work on */
290   if (is_binop(n)) {
291     a = get_binop_left(n);
292     b = get_binop_right(n);
293   } else if (is_unop(n)) {
294     a = get_unop_op(n);
295   }
296
297   /* skip unnecessary nodes. */
298   switch (get_irn_opcode (n)) {
299   case iro_Block:
300     {
301       /* The Block constructor does not call optimize, but mature_block
302          calls the optimization. */
303       assert(get_Block_matured(n));
304
305       /* Straightening: a single entry Block following a single exit Block
306          can be merged, if it is not the Start block. */
307       /* !!! Beware, all Phi-nodes of n must have been optimized away.
308          This should be true, as the block is matured before optimize is called.
309          But what about Phi-cycles with the Phi0/Id that could not be resolved?
310          Remaining Phi nodes are just Ids. */
311       if ((get_Block_n_cfgpreds(n) == 1) &&
312           (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp) &&
313           (get_opt_control_flow())) {
314         n = get_nodes_Block(get_Block_cfgpred(n, 0));                     DBG_OPT_STG;
315       } else if ((get_Block_n_cfgpreds(n) == 2) &&
316                  (get_opt_control_flow())) {
317         /* Test whether Cond jumps twice to this block
318            @@@ we could do this also with two loops finding two preds from several ones. */
319         a = get_Block_cfgpred(n, 0);
320         b = get_Block_cfgpred(n, 1);
321         if ((get_irn_op(a) == op_Proj) &&
322             (get_irn_op(b) == op_Proj) &&
323             (get_Proj_pred(a) == get_Proj_pred(b)) &&
324             (get_irn_op(get_Proj_pred(a)) == op_Cond)) {
325           /* Also a single entry Block following a single exit Block.  Phis have
326              twice the same operand and will be optimized away. */
327           n = get_nodes_Block(a);                                         DBG_OPT_IFSIM;
328         }
329       } else if (get_opt_unreachable_code() &&
330                  (n != current_ir_graph->start_block) &&
331                  (n != current_ir_graph->end_block)     ) {
332         int i;
333         /* If all inputs are dead, this block is dead too, except if it is
334            the start or end block.  This is a step of unreachable code
335            elimination */
336         for (i = 0; i < get_Block_n_cfgpreds(n); i++) {
337           if (!is_Bad(get_Block_cfgpred(n, i))) break;
338         }
339         if (i == get_Block_n_cfgpreds(n))
340           n = new_Bad();
341       }
342     }
343     break;
344
345   case iro_Jmp:  /* GL: Why not same for op_Raise?? */
346     /* unreachable code elimination */
347     if (is_Bad(get_nodes_Block(n)))  n = new_Bad();
348     break;
349         /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
350            See cases for iro_Cond and iro_Proj in transform_node. */
351         /** remove stuff as x+0, x*1 x&true ... constant expression evaluation **/
352   case iro_Or:  if (a == b) {n = a; break;}
353   case iro_Add:
354   case iro_Eor: {
355     tarval *tv;
356     ir_node *on;
357     /* After running compute_node there is only one constant predecessor.
358        Find this predecessors value and remember the other node: */
359     if ((tv = computed_value (a))) {
360       on = b;
361     } else if ((tv = computed_value (b))) {
362       on = a;
363     } else break;
364
365     /* If this predecessors constant value is zero, the operation is
366        unnecessary. Remove it: */
367     if (tarval_classify (tv) == 0) {
368       n = on;                                                             DBG_OPT_ALGSIM1;
369     }
370   } break;
371   case iro_Sub:
372   case iro_Shl:
373   case iro_Shr:
374   case iro_Shrs:
375   case iro_Rot:
376     /* these operations are not commutative.  Test only one predecessor. */
377     if (tarval_classify (computed_value (b)) == 0) {
378       n = a;                                                              DBG_OPT_ALGSIM1;
379       /* Test if b > #bits of a ==> return 0 / divide b by #bits
380          --> transform node? */
381     }
382     break;
383   case iro_Not:   /* NotNot x == x */
384   case iro_Minus: /* --x == x */  /* ??? Is this possible or can --x raise an
385                                          out of bounds exception if min =! max? */
386     if (get_irn_op(get_unop_op(n)) == get_irn_op(n)) {
387       n = get_unop_op(get_unop_op(n));                                    DBG_OPT_ALGSIM2
388     }
389     break;
390   case iro_Mul:
391     /* Mul is commutative and has again an other neutral element. */
392     if (tarval_classify (computed_value (a)) == 1) {
393       n = b;                                                              DBG_OPT_ALGSIM1
394     } else if (tarval_classify (computed_value (b)) == 1) {
395       n = a;                                                              DBG_OPT_ALGSIM1
396     }
397     break;
398   case iro_Div:
399     /* Div is not commutative. */
400     if (tarval_classify (computed_value (b)) == 1) { /* div(x, 1) == x */
401       /* Turn Div into a tuple (mem, bad, a) */
402       ir_node *mem = get_Div_mem(n);
403       turn_into_tuple(n, 3);
404       set_Tuple_pred(n, 0, mem);
405       set_Tuple_pred(n, 1, new_Bad());
406       set_Tuple_pred(n, 2, a);
407     }
408     break;
409     /* GL: Why are they skipped?  DivMod allocates new nodes --> it's
410        treated in transform node.
411            case iro_Mod, Quot, DivMod
412     */
413   case iro_And:
414     if (a == b) {
415       n = a;    /* And has it's own neutral element */
416     } else if (tarval_classify (computed_value (a)) == -1) {
417       n = b;
418     } else if (tarval_classify (computed_value (b)) == -1) {
419       n = a;
420     }
421     if (n != oldn)                                                        DBG_OPT_ALGSIM1;
422     break;
423   case iro_Conv:
424     if (get_irn_mode(n) == get_irn_mode(a)) { /* No Conv necessary */
425       n = a;                                                              DBG_OPT_ALGSIM3;
426     } else if (get_irn_mode(n) == mode_b) {
427       if (get_irn_op(a) == op_Conv &&
428           get_irn_mode (get_Conv_op(a)) == mode_b) {
429         n = get_Conv_op(a);     /* Convb(Conv*(xxxb(...))) == xxxb(...) */ DBG_OPT_ALGSIM2;
430       }
431     }
432     break;
433
434   case iro_Phi:
435     {
436       /* Several optimizations:
437          - no Phi in start block.
438          - remove Id operators that are inputs to Phi
439          - fold Phi-nodes, iff they have only one predecessor except
440                  themselves.
441       */
442       int i, n_preds;
443
444       ir_node *block = NULL;     /* to shutup gcc */
445       ir_node *first_val = NULL; /* to shutup gcc */
446       ir_node *scnd_val = NULL;  /* to shutup gcc */
447
448       n_preds = get_Phi_n_preds(n);
449
450       block = get_nodes_Block(n);
451       if ((is_Bad(block)) ||                         /* Control dead */
452           (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
453         return new_Bad();                            /* in the Start Block. */
454
455       if (n_preds == 0) break;           /* Phi of dead Region without predecessors. */
456
457 #if 0
458       /* first we test for a special case: */
459       /* Confirm is a special node fixing additional information for a
460          value that is known at a certain point.  This is useful for
461          dataflow analysis. */
462       if (n_preds == 2) {
463         ir_node *a = follow_Id (get_Phi_pred(n, 0));
464         ir_node *b = follow_Id (get_Phi_pred(n, 1));
465         if (   (get_irn_op(a) == op_Confirm)
466             && (get_irn_op(b) == op_Confirm)
467             && (follow_Id (get_irn_n(a, 0)) == follow_Id(get_irn_n(b, 0)))
468             && (get_irn_n(a, 1) == get_irn_n (b, 1))
469             && (a->data.num == (~b->data.num & irpn_True) )) {
470           n = follow_Id (get_irn_n(a, 0));
471           break;
472         }
473       }
474 #endif
475
476       /* Find first non-self-referencing input */
477       for (i = 0;  i < n_preds;  ++i) {
478         first_val = follow_Id(get_Phi_pred(n, i));
479         /* skip Id's */
480         set_Phi_pred(n, i, first_val);
481         if (   (first_val != n)                            /* not self pointer */
482             && (get_irn_op(first_val) != op_Bad)           /* value not dead */
483             && !(is_Bad (get_Block_cfgpred(block, i))) ) { /* not dead control flow */
484           break;                         /* then found first value. */
485         }
486       }
487
488       /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
489       if (i >= n_preds) { n = new_Bad();  break; }
490
491       scnd_val = NULL;
492
493       /* follow_Id () for rest of inputs, determine if any of these
494          are non-self-referencing */
495       while (++i < n_preds) {
496         scnd_val = follow_Id(get_Phi_pred(n, i));
497         /* skip Id's */
498         set_Phi_pred(n, i, scnd_val);
499         if (   (scnd_val != n)
500             && (scnd_val != first_val)
501             && (get_irn_op(scnd_val) != op_Bad)
502             && !(is_Bad (get_Block_cfgpred(block, i))) ) {
503           break;
504         }
505       }
506
507       /* Fold, if no multiple distinct non-self-referencing inputs */
508       if (i >= n_preds) {
509         n = first_val;                                     DBG_OPT_PHI;
510       } else {
511         /* skip the remaining Ids. */
512         while (++i < n_preds) {
513           set_Phi_pred(n, i, follow_Id(get_Phi_pred(n, i)));
514         }
515       }
516     }
517     break;
518
519   case iro_Load:
520     {
521 #if 0  /* Is an illegal transformation: different nodes can
522           represent the same pointer value!! */
523  a = skip_Proj(get_Load_mem(n));
524  b = get_Load_ptr(n);
525
526  if (get_irn_op(a) == op_Store) {
527    if ( different_identity (b, get_Store_ptr(a))) {
528          /* load and store use different pointers, therefore load
529                 needs not take store's memory but the state before. */
530          set_Load_mem (n, get_Store_mem(a));
531    } else if (( 0 /* ???didn't get cryptic test that returns 0 */ )) {
532    }
533  }
534 #endif
535     }
536         break;
537   case iro_Store:
538     /* remove unnecessary store. */
539     {
540       a = skip_Proj(get_Store_mem(n));
541       b = get_Store_ptr(n);
542       c = skip_Proj(get_Store_value(n));
543
544       if (get_irn_op(a) == op_Store
545           && get_Store_ptr(a) == b
546           && skip_Proj(get_Store_value(a)) == c) {
547         /* We have twice exactly the same store -- a write after write. */
548         n = a;                                                         DBG_OPT_WAW;
549       } else if (get_irn_op(c) == op_Load
550                  && (a == c || skip_Proj(get_Load_mem(c)) == a)
551                  && get_Load_ptr(c) == b ) {
552         /* We just loaded the value from the same memory, i.e., the store
553            doesn't change the memory -- a write after read. */
554         a = get_Store_mem(n);
555         turn_into_tuple(n, 2);
556         set_Tuple_pred(n, 0, a);
557         set_Tuple_pred(n, 1, new_Bad());                               DBG_OPT_WAR;
558        }
559     }
560     break;
561
562   case iro_Proj:
563     {
564       a = get_Proj_pred(n);
565
566       if ( get_irn_op(a) == op_Tuple) {
567         /* Remove the Tuple/Proj combination. */
568         if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
569           n = get_Tuple_pred(a, get_Proj_proj(n));                     DBG_OPT_TUPLE;
570         } else {
571           assert(0); /* This should not happen! */
572           n = new_Bad();
573         }
574       } else if (get_irn_mode(n) == mode_X &&
575                  is_Bad(get_nodes_Block(n))) {
576         /* Remove dead control flow. */
577         n = new_Bad();
578       }
579     }
580     break;
581
582   case iro_Id:
583     n = follow_Id (n);                                                 DBG_OPT_ID;
584     break;
585
586   default: break;
587   }
588
589   return n;
590 } /* end equivalent_node() */
591
592
593 /* tries several [inplace] [optimizing] transformations and returns an
594    equivalent node.  The difference to equivalent_node is that these
595    transformations _do_ generate new nodes, and thus the old node must
596    not be freed even if the equivalent node isn't the old one. */
597 static ir_node *
598 transform_node (ir_node *n)
599 {
600
601   ir_node *a = NULL, *b;
602   tarval *ta, *tb;
603
604   switch (get_irn_opcode(n)) {
605   case iro_Div: {
606     ta = computed_value(n);
607     if (ta) {
608       /* Turn Div into a tuple (mem, bad, value) */
609       ir_node *mem = get_Div_mem(n);
610       turn_into_tuple(n, 3);
611       set_Tuple_pred(n, 0, mem);
612       set_Tuple_pred(n, 1, new_Bad());
613       set_Tuple_pred(n, 2, new_Const(get_tv_mode(ta), ta));
614     }
615   } break;
616   case iro_Mod: {
617     ta = computed_value(n);
618     if (ta) {
619       /* Turn Div into a tuple (mem, bad, value) */
620       ir_node *mem = get_Mod_mem(n);
621       turn_into_tuple(n, 3);
622       set_Tuple_pred(n, 0, mem);
623       set_Tuple_pred(n, 1, new_Bad());
624       set_Tuple_pred(n, 2, new_Const(get_tv_mode(ta), ta));
625     }
626   } break;
627   case iro_DivMod: {
628
629     int evaluated = 0;
630     ir_mode *mode;
631
632     a = get_DivMod_left(n);
633     b = get_DivMod_right(n);
634     mode = get_irn_mode(a);
635
636     if (!(mode_is_int(get_irn_mode(a)) &&
637           mode_is_int(get_irn_mode(b))))
638       break;
639
640     if (a == b) {
641       a = new_Const (mode, tarval_from_long (mode, 1));
642       b = new_Const (mode, tarval_from_long (mode, 0));
643       evaluated = 1;
644     } else {
645       ta = value_of(a);
646       tb = value_of(b);
647
648       if (tb) {
649         if (tarval_classify(tb) == 1) {
650           b = new_Const (mode, tarval_from_long (mode, 0));
651           evaluated = 1;
652         } else if (ta) {
653           tarval *resa, *resb;
654           resa = tarval_div (ta, tb);
655           if (!resa) break; /* Causes exception!!! Model by replacing through
656                                Jmp for X result!? */
657           resb = tarval_mod (ta, tb);
658           if (!resb) break; /* Causes exception! */
659           a = new_Const (mode, resa);
660           b = new_Const (mode, resb);
661           evaluated = 1;
662         }
663       } else if (tarval_classify (ta) == 0) {
664         b = a;
665         evaluated = 1;
666       }
667     }
668     if (evaluated) { /* replace by tuple */
669       ir_node *mem = get_DivMod_mem(n);
670       turn_into_tuple(n, 4);
671       set_Tuple_pred(n, 0, mem);
672       set_Tuple_pred(n, 1, new_Bad());  /* no exception */
673       set_Tuple_pred(n, 2, a);
674       set_Tuple_pred(n, 3, b);
675       assert(get_nodes_Block(n));
676     }
677   }
678   break;
679
680   case iro_Cond: {
681     /* Replace the Cond by a Jmp if it branches on a constant
682        condition. */
683     ir_node *jmp;
684     a = get_Cond_selector(n);
685     ta = value_of(a);
686
687     if (ta &&
688         (get_irn_mode(a) == mode_b) &&
689         (get_opt_unreachable_code())) {
690       /* It's a boolean Cond, branching on a boolean constant.
691                  Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
692       jmp = new_r_Jmp(current_ir_graph, get_nodes_Block(n));
693       turn_into_tuple(n, 2);
694       if (tv_val_b(ta) == 1)  /* GL: I hope this returns 1 if true */ {
695                 set_Tuple_pred(n, 0, new_Bad());
696                 set_Tuple_pred(n, 1, jmp);
697       } else {
698                 set_Tuple_pred(n, 0, jmp);
699                 set_Tuple_pred(n, 1, new_Bad());
700       }
701       /* We might generate an endless loop, so keep it alive. */
702       add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
703     } else if (ta &&
704                (get_irn_mode(a) == mode_I) &&
705                (get_Cond_kind(n) == dense) &&
706                (get_opt_unreachable_code())) {
707       /* I don't want to allow Tuples smaller than the biggest Proj.
708          Also this tuple might get really big...
709          I generate the Jmp here, and remember it in link.  Link is used
710          when optimizing Proj. */
711       set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
712       /* We might generate an endless loop, so keep it alive. */
713       add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
714     } else if ((get_irn_op(get_Cond_selector(n)) == op_Eor)
715                && (get_irn_mode(get_Cond_selector(n)) == mode_b)
716                && (tarval_classify(computed_value(get_Eor_right(a))) == 1)) {
717       /* The Eor is a negate.  Generate a new Cond without the negate,
718          simulate the negate by exchanging the results. */
719       set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
720                                  get_Eor_left(a)));
721     } else if ((get_irn_op(get_Cond_selector(n)) == op_Not)
722                && (get_irn_mode(get_Cond_selector(n)) == mode_b)) {
723       /* A Not before the Cond.  Generate a new Cond without the Not,
724          simulate the Not by exchanging the results. */
725       set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
726                                  get_Not_op(a)));
727     }
728   }
729   break;
730
731   case iro_Proj: {
732     a = get_Proj_pred(n);
733
734     if ((get_irn_op(a) == op_Cond)
735         && get_irn_link(a)
736         && get_irn_op(get_irn_link(a)) == op_Cond) {
737       /* Use the better Cond if the Proj projs from a Cond which get's
738          its result from an Eor/Not. */
739       assert (((get_irn_op(get_Cond_selector(a)) == op_Eor)
740                || (get_irn_op(get_Cond_selector(a)) == op_Not))
741               && (get_irn_mode(get_Cond_selector(a)) == mode_b)
742               && (get_irn_op(get_irn_link(a)) == op_Cond)
743               && (get_Cond_selector(get_irn_link(a)) == get_Eor_left(get_Cond_selector(a))));
744       set_Proj_pred(n, get_irn_link(a));
745       if (get_Proj_proj(n) == 0)
746         set_Proj_proj(n, 1);
747       else
748         set_Proj_proj(n, 0);
749     } else if ((get_irn_op(a) == op_Cond)
750                && (get_irn_mode(get_Cond_selector(a)) == mode_I)
751                && value_of(a)
752                && (get_Cond_kind(a) == dense)
753                && (get_opt_unreachable_code())) {
754       /* The Cond is a Switch on a Constant */
755       if (get_Proj_proj(n) == tv_val_CHIL(value_of(a))) {
756         /* The always taken branch, reuse the existing Jmp. */
757         if (!get_irn_link(a)) /* well, if it exists ;-> */
758           set_irn_link(a, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
759         assert(get_irn_op(get_irn_link(a)) == op_Jmp);
760         n = get_irn_link(a);
761       } else {/* Not taken control flow, but be careful with the default! */
762         if (get_Proj_proj(n) < a->attr.c.default_proj){
763           /* a never taken branch */
764           n = new_Bad();
765         } else {
766           a->attr.c.default_proj = get_Proj_proj(n);
767         }
768       }
769     }
770   } break;
771   case iro_Eor: { /* @@@ not tested as boolean Eor not allowed any more. */
772     a = get_Eor_left(n);
773     b = get_Eor_right(n);
774
775     if ((get_irn_mode(n) == mode_b)
776         && (get_irn_op(a) == op_Proj)
777         && (get_irn_mode(a) == mode_b)
778         && (tarval_classify (computed_value (b)) == 1)
779         && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
780       /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
781       n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
782                      mode_b, get_negated_pnc(get_Proj_proj(a)));
783     else if ((get_irn_mode(n) == mode_b)
784              && (tarval_classify (computed_value (b)) == 1))
785       /* The Eor is a Not. Replace it by a Not. */
786       /*   ????!!!Extend to bitfield 1111111. */
787       n = new_r_Not(current_ir_graph, get_nodes_Block(n), a, mode_b);
788   }
789   break;
790   case iro_Not: {
791     a = get_Not_op(n);
792
793     if (   (get_irn_mode(n) == mode_b)
794         && (get_irn_op(a) == op_Proj)
795         && (get_irn_mode(a) == mode_b)
796         && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
797       /* We negate a Cmp. The Cmp has the negated result anyways! */
798       n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
799                      mode_b, get_negated_pnc(get_Proj_proj(a)));
800   }
801   break;
802   default: ;
803   }
804   return n;
805 }
806
807 /* **************** Common Subexpression Elimination **************** */
808
809 /* Compare function for two nodes in the hash table.   Gets two       */
810 /* nodes as parameters.  Returns 0 if the nodes are a cse.            */
811 static int
812 vt_cmp (const void *elt, const void *key)
813 {
814   ir_node *a, *b;
815   int i;
816
817   a = (void *)elt;
818   b = (void *)key;
819
820   if (a == b) return 0;
821
822   if ((get_irn_op(a) != get_irn_op(b)) ||
823       (get_irn_mode(a) != get_irn_mode(b))) return 1;
824
825   /* compare if a's in and b's in are equal */
826   if (get_irn_arity (a) != get_irn_arity(b))
827     return 1;
828
829   /* for block-local cse and pinned nodes: */
830   if (!get_opt_global_cse() || (get_op_pinned(get_irn_op(a)) == pinned)) {
831     if (get_irn_n(a, -1) != get_irn_n(b, -1))
832       return 1;
833   }
834
835   /* compare a->in[0..ins] with b->in[0..ins] */
836   for (i = 0; i < get_irn_arity(a); i++)
837     if (get_irn_n(a, i) != get_irn_n(b, i))
838       return 1;
839
840   switch (get_irn_opcode(a)) {
841   case iro_Const:
842     return get_irn_const_attr (a) != get_irn_const_attr (b);
843   case iro_Proj:
844     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
845   case iro_Filter:
846     return get_Filter_proj(a) != get_Filter_proj(b);
847   case iro_Alloc:
848     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
849       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
850   case iro_Free:
851     return (get_irn_free_attr(a) != get_irn_free_attr(b));
852   case iro_SymConst:
853     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
854       || (get_irn_symconst_attr(a).tori.typ != get_irn_symconst_attr(b).tori.typ);
855   case iro_Call:
856     return (get_irn_call_attr(a) != get_irn_call_attr(b));
857   case iro_Sel:
858     return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
859       || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
860       || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
861       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
862       || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
863   case iro_Phi:
864     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
865   default: ;
866   }
867
868   return 0;
869 }
870
871 static unsigned
872 ir_node_hash (ir_node *node)
873 {
874   unsigned h;
875   int i;
876
877   /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
878   h = get_irn_arity(node);
879
880   /* consider all in nodes... except the block. */
881   for (i = 0;  i < get_irn_arity(node);  i++) {
882     h = 9*h + (unsigned long)get_irn_n(node, i);
883   }
884
885   /* ...mode,... */
886   h = 9*h + (unsigned long) get_irn_mode (node);
887   /* ...and code */
888   h = 9*h + (unsigned long) get_irn_op (node);
889
890   return h;
891 }
892
893 pset *
894 new_identities (void)
895 {
896   return new_pset (vt_cmp, TUNE_NIR_NODES);
897 }
898
899 void
900 del_identities (pset *value_table)
901 {
902   del_pset (value_table);
903 }
904
905 /* Return the canonical node computing the same value as n.
906    Looks up the node in a hash table. */
907 static INLINE ir_node *
908 identify (pset *value_table, ir_node *n)
909 {
910   ir_node *o = NULL;
911
912   if (!value_table) return n;
913
914   if (get_opt_reassociation()) {
915     switch (get_irn_opcode (n)) {
916     case iro_Add:
917     case iro_Mul:
918     case iro_Or:
919     case iro_And:
920     case iro_Eor:
921       {
922         /* for commutative operators perform  a OP b == b OP a */
923         if (get_binop_left(n) > get_binop_right(n)) {
924           ir_node *h = get_binop_left(n);
925           set_binop_left(n, get_binop_right(n));
926           set_binop_right(n, h);
927         }
928       }
929       break;
930     default: break;
931     }
932   }
933
934   o = pset_find (value_table, n, ir_node_hash (n));
935   if (!o) return n;
936
937   return o;
938 }
939
940 /* During construction we set the pinned flag in the graph right when the
941    optimizatin is performed.  The flag turning on procedure global cse could
942    be changed between two allocations.  This way we are safe. */
943 static INLINE ir_node *
944 identify_cons (pset *value_table, ir_node *n) {
945   ir_node *old = n;
946   n = identify(value_table, n);
947   if (get_irn_n(old, -1) != get_irn_n(n, -1))
948     set_irg_pinned(current_ir_graph, floats);
949   return n;
950 }
951
952 /* Return the canonical node computing the same value as n.
953    Looks up the node in a hash table, enters it in the table
954    if it isn't there yet. */
955 static ir_node *
956 identify_remember (pset *value_table, ir_node *node)
957 {
958   ir_node *o = NULL;
959
960   if (!value_table) return node;
961
962   /* lookup or insert in hash table with given hash key. */
963   o = pset_insert (value_table, node, ir_node_hash (node));
964
965   if (o == node) return node;
966
967   return o;
968 }
969
970 void
971 add_identities (pset *value_table, ir_node *node) {
972   identify_remember (value_table, node);
973 }
974
975 /* garbage in, garbage out. If a node has a dead input, i.e., the
976    Bad node is input to the node, return the Bad node.  */
977 static INLINE ir_node *
978 gigo (ir_node *node)
979 {
980   int i;
981   ir_op* op = get_irn_op(node);
982
983   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
984      blocks predecessors is dead. */
985   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
986     for (i = -1; i < get_irn_arity(node); i++) {
987       if (is_Bad(get_irn_n(node, i))) {
988         return new_Bad();
989       }
990     }
991   }
992 #if 0
993   /* If Block has only Bads as predecessors it's garbage. */
994   /* If Phi has only Bads as predecessors it's garbage. */
995   if (op == op_Block || op == op_Phi)  {
996     for (i = 0; i < get_irn_arity(node); i++) {
997       if (!is_Bad(get_irn_n(node, i))) break;
998     }
999     if (i = get_irn_arity(node)) node = new_Bad();
1000   }
1001 #endif
1002   return node;
1003 }
1004
1005
1006 /* These optimizations deallocate nodes from the obstack.
1007    It can only be called if it is guaranteed that no other nodes
1008    reference this one, i.e., right after construction of a node.  */
1009 ir_node *
1010 optimize (ir_node *n)
1011 {
1012   tarval *tv;
1013   ir_node *old_n = n;
1014
1015   /* Allways optimize Phi nodes: part of the construction. */
1016   if ((!get_optimize()) && (get_irn_op(n) != op_Phi)) return n;
1017
1018   /* constant expression evaluation / constant folding */
1019   if (get_opt_constant_folding()) {
1020     /* constants can not be evaluated */
1021     if  (get_irn_op(n) != op_Const) {
1022       /* try to evaluate */
1023       tv = computed_value (n);
1024       if ((get_irn_mode(n) != mode_T) && (tv != NULL)) {
1025         /* evaluation was succesful -- replace the node. */
1026         obstack_free (current_ir_graph->obst, n);
1027         return new_Const (get_tv_mode (tv), tv);
1028       }
1029     }
1030   }
1031
1032   /* remove unnecessary nodes */
1033   if (get_opt_constant_folding() ||
1034       (get_irn_op(n) == op_Phi)  ||   /* always optimize these nodes. */
1035       (get_irn_op(n) == op_Id)   ||
1036       (get_irn_op(n) == op_Proj) ||
1037       (get_irn_op(n) == op_Block)  )  /* Flags tested local. */
1038     n = equivalent_node (n);
1039
1040   /** common subexpression elimination **/
1041   /* Checks whether n is already available. */
1042   /* The block input is used to distinguish different subexpressions. Right
1043      now all nodes are pinned to blocks, i.e., the cse only finds common
1044      subexpressions within a block. */
1045   if (get_opt_cse())
1046     n = identify_cons (current_ir_graph->value_table, n);
1047
1048   if (n != old_n) {
1049     /* We found an existing, better node, so we can deallocate the old node. */
1050     obstack_free (current_ir_graph->obst, old_n);
1051   }
1052
1053   /* Some more constant expression evaluation that does not allow to
1054      free the node. */
1055   if (get_opt_constant_folding() ||
1056       (get_irn_op(n) == op_Cond) ||
1057       (get_irn_op(n) == op_Proj))     /* Flags tested local. */
1058     n = transform_node (n);
1059
1060   /* Remove nodes with dead (Bad) input.
1061      Run always for transformation induced Bads. */
1062   n = gigo (n);
1063
1064   /* Now we can verify the node, as it has no dead inputs any more. */
1065   irn_vrfy(n);
1066
1067   /* Now we have a legal, useful node. Enter it in hash table for cse */
1068   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
1069     n = identify_remember (current_ir_graph->value_table, n);
1070   }
1071
1072   return n;
1073 }
1074
1075
1076 /* These optimizations never deallocate nodes.  This can cause dead
1077    nodes lying on the obstack.  Remove these by a dead node elimination,
1078    i.e., a copying garbage collection. */
1079 ir_node *
1080 optimize_in_place_2 (ir_node *n)
1081 {
1082   tarval *tv;
1083   ir_node *old_n = n;
1084
1085   if (!get_optimize() && (get_irn_op(n) != op_Phi)) return n;
1086
1087   /* if not optimize return n */
1088   if (n == NULL) {
1089     assert(0);
1090     /* Here this is possible.  Why? */
1091     return n;
1092   }
1093
1094
1095   /* constant expression evaluation / constant folding */
1096   if (get_opt_constant_folding()) {
1097     /* constants can not be evaluated */
1098     if  (get_irn_op(n) != op_Const) {
1099       /* try to evaluate */
1100       tv = computed_value (n);
1101       if ((get_irn_mode(n) != mode_T) && (tv != NULL)) {
1102         /* evaluation was succesful -- replace the node. */
1103         n = new_Const (get_tv_mode (tv), tv);
1104         __dbg_info_merge_pair(n, old_n, dbg_const_eval);
1105         return n;
1106       }
1107     }
1108   }
1109
1110   /* remove unnecessary nodes */
1111   /*if (get_opt_constant_folding()) */
1112   if (get_opt_constant_folding() ||
1113       (get_irn_op(n) == op_Phi)  ||   /* always optimize these nodes. */
1114       (get_irn_op(n) == op_Id)   ||   /* ... */
1115       (get_irn_op(n) == op_Proj) ||   /* ... */
1116       (get_irn_op(n) == op_Block)  )  /* Flags tested local. */
1117     n = equivalent_node (n);
1118
1119   /** common subexpression elimination **/
1120   /* Checks whether n is already available. */
1121   /* The block input is used to distinguish different subexpressions.  Right
1122      now all nodes are pinned to blocks, i.e., the cse only finds common
1123      subexpressions within a block. */
1124   if (get_opt_cse()) {
1125     n = identify (current_ir_graph->value_table, n);
1126   }
1127
1128   /* Some more constant expression evaluation. */
1129   if (get_opt_constant_folding() ||
1130       (get_irn_op(n) == op_Cond) ||
1131       (get_irn_op(n) == op_Proj))     /* Flags tested local. */
1132     n = transform_node (n);
1133
1134   /* Remove nodes with dead (Bad) input.
1135      Run always for transformation induced Bads.  */
1136   n = gigo (n);
1137
1138   /* Now we can verify the node, as it has no dead inputs any more. */
1139   irn_vrfy(n);
1140
1141   /* Now we have a legal, useful node. Enter it in hash table for cse.
1142      Blocks should be unique anyways.  (Except the successor of start:
1143      is cse with the start block!) */
1144   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
1145     n = identify_remember (current_ir_graph->value_table, n);
1146
1147   return n;
1148 }
1149
1150 /* Wrapper for external use, set proper status bits after optimization */
1151 ir_node *
1152 optimize_in_place (ir_node *n) {
1153   /* Handle graph state */
1154   assert(get_irg_phase_state(current_ir_graph) != phase_building);
1155   if (get_opt_global_cse())
1156     set_irg_pinned(current_ir_graph, floats);
1157   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1158     set_irg_outs_inconsistent(current_ir_graph);
1159   /* Maybe we could also test whether optimizing the node can
1160      change the control graph. */
1161   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1162     set_irg_dom_inconsistent(current_ir_graph);
1163   return optimize_in_place_2 (n);
1164 }