added doxygen comments
[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_Is, 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, greater equal, ... this will return true, else it
184             will return false.  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 static 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_straightening())) {
314         n = get_nodes_Block(get_Block_cfgpred(n, 0));                     DBG_OPT_STG;
315
316       } else if ((get_Block_n_cfgpreds(n) == 2) &&
317                  (get_opt_control_flow_weak_simplification())) {
318         /* Test whether Cond jumps twice to this block
319            @@@ we could do this also with two loops finding two preds from several ones. */
320         a = get_Block_cfgpred(n, 0);
321         b = get_Block_cfgpred(n, 1);
322         if ((get_irn_op(a) == op_Proj) &&
323             (get_irn_op(b) == op_Proj) &&
324             (get_Proj_pred(a) == get_Proj_pred(b)) &&
325             (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
326             (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
327           /* Also a single entry Block following a single exit Block.  Phis have
328              twice the same operand and will be optimized away. */
329           n = get_nodes_Block(a);                                         DBG_OPT_IFSIM;
330         }
331       } else if (get_opt_unreachable_code() &&
332                  (n != current_ir_graph->start_block) &&
333                  (n != current_ir_graph->end_block)     ) {
334         int i;
335         /* If all inputs are dead, this block is dead too, except if it is
336            the start or end block.  This is a step of unreachable code
337            elimination */
338         for (i = 0; i < get_Block_n_cfgpreds(n); i++) {
339           if (!is_Bad(get_Block_cfgpred(n, i))) break;
340         }
341         if (i == get_Block_n_cfgpreds(n))
342           n = new_Bad();
343       }
344     }
345     break;
346
347   case iro_Jmp:  /* GL: Why not same for op_Raise?? */
348     /* unreachable code elimination */
349     if (is_Bad(get_nodes_Block(n)))  n = new_Bad();
350     break;
351         /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
352            See cases for iro_Cond and iro_Proj in transform_node. */
353         /** remove stuff as x+0, x*1 x&true ... constant expression evaluation **/
354   case iro_Or:  if (a == b) {n = a; break;}
355   case iro_Add:
356   case iro_Eor: {
357     tarval *tv;
358     ir_node *on;
359     /* After running compute_node there is only one constant predecessor.
360        Find this predecessors value and remember the other node: */
361     if ((tv = computed_value (a))) {
362       on = b;
363     } else if ((tv = computed_value (b))) {
364       on = a;
365     } else break;
366
367     /* If this predecessors constant value is zero, the operation is
368        unnecessary. Remove it: */
369     if (tarval_classify (tv) == 0) {
370       n = on;                                                             DBG_OPT_ALGSIM1;
371     }
372   } break;
373   case iro_Sub:
374   case iro_Shl:
375   case iro_Shr:
376   case iro_Shrs:
377   case iro_Rot:
378     /* these operations are not commutative.  Test only one predecessor. */
379     if (tarval_classify (computed_value (b)) == 0) {
380       n = a;                                                              DBG_OPT_ALGSIM1;
381       /* Test if b > #bits of a ==> return 0 / divide b by #bits
382          --> transform node? */
383     }
384     break;
385   case iro_Not:   /* NotNot x == x */
386   case iro_Minus: /* --x == x */  /* ??? Is this possible or can --x raise an
387                                          out of bounds exception if min =! max? */
388     if (get_irn_op(get_unop_op(n)) == get_irn_op(n)) {
389       n = get_unop_op(get_unop_op(n));                                    DBG_OPT_ALGSIM2
390     }
391     break;
392   case iro_Mul:
393     /* Mul is commutative and has again an other neutral element. */
394     if (tarval_classify (computed_value (a)) == 1) {
395       n = b;                                                              DBG_OPT_ALGSIM1
396     } else if (tarval_classify (computed_value (b)) == 1) {
397       n = a;                                                              DBG_OPT_ALGSIM1
398     }
399     break;
400   case iro_Div:
401     /* Div is not commutative. */
402     if (tarval_classify (computed_value (b)) == 1) { /* div(x, 1) == x */
403       /* Turn Div into a tuple (mem, bad, a) */
404       ir_node *mem = get_Div_mem(n);
405       turn_into_tuple(n, 3);
406       set_Tuple_pred(n, 0, mem);
407       set_Tuple_pred(n, 1, new_Bad());
408       set_Tuple_pred(n, 2, a);
409     }
410     break;
411   /*
412   case iro_Mod, Quot, DivMod
413     DivMod allocates new nodes --> it's treated in transform node.
414     What about Quot, DivMod?
415   */
416   case iro_And:
417     if (a == b) {
418       n = a;    /* And has it's own neutral element */
419     } else if (tarval_classify (computed_value (a)) == -1) {
420       n = b;
421     } else if (tarval_classify (computed_value (b)) == -1) {
422       n = a;
423     }
424     if (n != oldn)                                                        DBG_OPT_ALGSIM1;
425     break;
426   case iro_Conv:
427     if (get_irn_mode(n) == get_irn_mode(a)) { /* No Conv necessary */
428       n = a;                                                              DBG_OPT_ALGSIM3;
429     } else if (get_irn_mode(n) == mode_b) {
430       if (get_irn_op(a) == op_Conv &&
431           get_irn_mode (get_Conv_op(a)) == mode_b) {
432         n = get_Conv_op(a);     /* Convb(Conv*(xxxb(...))) == xxxb(...) */ DBG_OPT_ALGSIM2;
433       }
434     }
435     break;
436
437   case iro_Phi:
438     {
439       /* Several optimizations:
440          - no Phi in start block.
441          - remove Id operators that are inputs to Phi
442          - fold Phi-nodes, iff they have only one predecessor except
443                  themselves.
444       */
445       int i, n_preds;
446
447       ir_node *block = NULL;     /* to shutup gcc */
448       ir_node *first_val = NULL; /* to shutup gcc */
449       ir_node *scnd_val = NULL;  /* to shutup gcc */
450
451       n_preds = get_Phi_n_preds(n);
452
453       block = get_nodes_Block(n);
454       /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
455          assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
456       if ((is_Bad(block)) ||                         /* Control dead */
457           (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
458         return new_Bad();                            /* in the Start Block. */
459
460       if (n_preds == 0) break;           /* Phi of dead Region without predecessors. */
461
462 #if 0
463       /* first we test for a special case: */
464       /* Confirm is a special node fixing additional information for a
465          value that is known at a certain point.  This is useful for
466          dataflow analysis. */
467       if (n_preds == 2) {
468         ir_node *a = follow_Id (get_Phi_pred(n, 0));
469         ir_node *b = follow_Id (get_Phi_pred(n, 1));
470         if (   (get_irn_op(a) == op_Confirm)
471             && (get_irn_op(b) == op_Confirm)
472             && (follow_Id (get_irn_n(a, 0)) == follow_Id(get_irn_n(b, 0)))
473             && (get_irn_n(a, 1) == get_irn_n (b, 1))
474             && (a->data.num == (~b->data.num & irpn_True) )) {
475           n = follow_Id (get_irn_n(a, 0));
476           break;
477         }
478       }
479 #endif
480
481       /* Find first non-self-referencing input */
482       for (i = 0;  i < n_preds;  ++i) {
483         first_val = follow_Id(get_Phi_pred(n, i));
484         /* skip Id's */
485         set_Phi_pred(n, i, first_val);
486         if (   (first_val != n)                            /* not self pointer */
487             && (get_irn_op(first_val) != op_Bad)           /* value not dead */
488             && !(is_Bad (get_Block_cfgpred(block, i))) ) { /* not dead control flow */
489           break;                         /* then found first value. */
490         }
491       }
492
493       /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
494       if (i >= n_preds) { n = new_Bad();  break; }
495
496       scnd_val = NULL;
497
498       /* follow_Id () for rest of inputs, determine if any of these
499          are non-self-referencing */
500       while (++i < n_preds) {
501         scnd_val = follow_Id(get_Phi_pred(n, i));
502         /* skip Id's */
503         set_Phi_pred(n, i, scnd_val);
504         if (   (scnd_val != n)
505             && (scnd_val != first_val)
506             && (get_irn_op(scnd_val) != op_Bad)
507             && !(is_Bad (get_Block_cfgpred(block, i))) ) {
508           break;
509         }
510       }
511
512       /* Fold, if no multiple distinct non-self-referencing inputs */
513       if (i >= n_preds) {
514         n = first_val;                                     DBG_OPT_PHI;
515       } else {
516         /* skip the remaining Ids. */
517         while (++i < n_preds) {
518           set_Phi_pred(n, i, follow_Id(get_Phi_pred(n, i)));
519         }
520       }
521     }
522     break;
523
524   case iro_Load:
525     {
526 #if 0  /* Is an illegal transformation: different nodes can
527           represent the same pointer value!! */
528  a = skip_Proj(get_Load_mem(n));
529  b = get_Load_ptr(n);
530
531  if (get_irn_op(a) == op_Store) {
532    if ( different_identity (b, get_Store_ptr(a))) {
533          /* load and store use different pointers, therefore load
534                 needs not take store's memory but the state before. */
535          set_Load_mem (n, get_Store_mem(a));
536    } else if (( 0 /* ???didn't get cryptic test that returns 0 */ )) {
537    }
538  }
539 #endif
540     }
541         break;
542   case iro_Store:
543     /* remove unnecessary store. */
544     {
545       a = skip_Proj(get_Store_mem(n));
546       b = get_Store_ptr(n);
547       c = skip_Proj(get_Store_value(n));
548
549       if (get_irn_op(a) == op_Store
550           && get_Store_ptr(a) == b
551           && skip_Proj(get_Store_value(a)) == c) {
552         /* We have twice exactly the same store -- a write after write. */
553         n = a;                                                         DBG_OPT_WAW;
554       } else if (get_irn_op(c) == op_Load
555                  && (a == c || skip_Proj(get_Load_mem(c)) == a)
556                  && get_Load_ptr(c) == b ) {
557         /* We just loaded the value from the same memory, i.e., the store
558            doesn't change the memory -- a write after read. */
559         a = get_Store_mem(n);
560         turn_into_tuple(n, 2);
561         set_Tuple_pred(n, 0, a);
562         set_Tuple_pred(n, 1, new_Bad());                               DBG_OPT_WAR;
563        }
564     }
565     break;
566
567   case iro_Proj:
568     {
569       a = get_Proj_pred(n);
570
571       if ( get_irn_op(a) == op_Tuple) {
572         /* Remove the Tuple/Proj combination. */
573         if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
574           n = get_Tuple_pred(a, get_Proj_proj(n));                     DBG_OPT_TUPLE;
575         } else {
576           assert(0); /* This should not happen! */
577           n = new_Bad();
578         }
579       } else if (get_irn_mode(n) == mode_X &&
580                  is_Bad(get_nodes_Block(n))) {
581         /* Remove dead control flow -- early gigo. */
582         n = new_Bad();
583       }
584     }
585     break;
586
587   case iro_Id:
588     n = follow_Id (n);                                                 DBG_OPT_ID;
589     break;
590
591   default: break;
592   }
593
594   return n;
595 } /* end equivalent_node() */
596
597
598 /* tries several [inplace] [optimizing] transformations and returns an
599    equivalent node.  The difference to equivalent_node is that these
600    transformations _do_ generate new nodes, and thus the old node must
601    not be freed even if the equivalent node isn't the old one. */
602 static ir_node *
603 transform_node (ir_node *n)
604 {
605
606   ir_node *a = NULL, *b;
607   tarval *ta, *tb;
608
609   switch (get_irn_opcode(n)) {
610   case iro_Div: {
611     ta = computed_value(n);
612     if (ta) {
613       /* Turn Div into a tuple (mem, bad, value) */
614       ir_node *mem = get_Div_mem(n);
615       turn_into_tuple(n, 3);
616       set_Tuple_pred(n, 0, mem);
617       set_Tuple_pred(n, 1, new_Bad());
618       set_Tuple_pred(n, 2, new_Const(get_tv_mode(ta), ta));
619     }
620   } break;
621   case iro_Mod: {
622     ta = computed_value(n);
623     if (ta) {
624       /* Turn Div into a tuple (mem, bad, value) */
625       ir_node *mem = get_Mod_mem(n);
626       turn_into_tuple(n, 3);
627       set_Tuple_pred(n, 0, mem);
628       set_Tuple_pred(n, 1, new_Bad());
629       set_Tuple_pred(n, 2, new_Const(get_tv_mode(ta), ta));
630     }
631   } break;
632   case iro_DivMod: {
633
634     int evaluated = 0;
635     ir_mode *mode;
636
637     a = get_DivMod_left(n);
638     b = get_DivMod_right(n);
639     mode = get_irn_mode(a);
640
641     if (!(mode_is_int(get_irn_mode(a)) &&
642           mode_is_int(get_irn_mode(b))))
643       break;
644
645     if (a == b) {
646       a = new_Const (mode, tarval_from_long (mode, 1));
647       b = new_Const (mode, tarval_from_long (mode, 0));
648       evaluated = 1;
649     } else {
650       ta = value_of(a);
651       tb = value_of(b);
652
653       if (tb) {
654         if (tarval_classify(tb) == 1) {
655           b = new_Const (mode, tarval_from_long (mode, 0));
656           evaluated = 1;
657         } else if (ta) {
658           tarval *resa, *resb;
659           resa = tarval_div (ta, tb);
660           if (!resa) break; /* Causes exception!!! Model by replacing through
661                                Jmp for X result!? */
662           resb = tarval_mod (ta, tb);
663           if (!resb) break; /* Causes exception! */
664           a = new_Const (mode, resa);
665           b = new_Const (mode, resb);
666           evaluated = 1;
667         }
668       } else if (tarval_classify (ta) == 0) {
669         b = a;
670         evaluated = 1;
671       }
672     }
673     if (evaluated) { /* replace by tuple */
674       ir_node *mem = get_DivMod_mem(n);
675       turn_into_tuple(n, 4);
676       set_Tuple_pred(n, 0, mem);
677       set_Tuple_pred(n, 1, new_Bad());  /* no exception */
678       set_Tuple_pred(n, 2, a);
679       set_Tuple_pred(n, 3, b);
680       assert(get_nodes_Block(n));
681     }
682   }
683   break;
684
685   case iro_Cond: {
686     /* Replace the Cond by a Jmp if it branches on a constant
687        condition. */
688     ir_node *jmp;
689     a = get_Cond_selector(n);
690     ta = value_of(a);
691
692     if (ta &&
693         (get_irn_mode(a) == mode_b) &&
694         (get_opt_unreachable_code())) {
695       /* It's a boolean Cond, branching on a boolean constant.
696                  Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
697       jmp = new_r_Jmp(current_ir_graph, get_nodes_Block(n));
698       turn_into_tuple(n, 2);
699       if (tv_val_b(ta) == 1)  /* GL: I hope this returns 1 if true */ {
700                 set_Tuple_pred(n, 0, new_Bad());
701                 set_Tuple_pred(n, 1, jmp);
702       } else {
703                 set_Tuple_pred(n, 0, jmp);
704                 set_Tuple_pred(n, 1, new_Bad());
705       }
706       /* We might generate an endless loop, so keep it alive. */
707       add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
708     } else if (ta &&
709                (get_irn_mode(a) == mode_Iu) &&
710                (get_Cond_kind(n) == dense) &&
711                (get_opt_unreachable_code())) {
712       /* I don't want to allow Tuples smaller than the biggest Proj.
713          Also this tuple might get really big...
714          I generate the Jmp here, and remember it in link.  Link is used
715          when optimizing Proj. */
716       set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
717       /* We might generate an endless loop, so keep it alive. */
718       add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
719     } else if ((get_irn_op(get_Cond_selector(n)) == op_Eor)
720                && (get_irn_mode(get_Cond_selector(n)) == mode_b)
721                && (tarval_classify(computed_value(get_Eor_right(a))) == 1)) {
722       /* The Eor is a negate.  Generate a new Cond without the negate,
723          simulate the negate by exchanging the results. */
724       set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
725                                  get_Eor_left(a)));
726     } else if ((get_irn_op(get_Cond_selector(n)) == op_Not)
727                && (get_irn_mode(get_Cond_selector(n)) == mode_b)) {
728       /* A Not before the Cond.  Generate a new Cond without the Not,
729          simulate the Not by exchanging the results. */
730       set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
731                                  get_Not_op(a)));
732     }
733   }
734   break;
735
736   case iro_Proj: {
737     a = get_Proj_pred(n);
738
739     if ((get_irn_op(a) == op_Cond)
740         && get_irn_link(a)
741         && get_irn_op(get_irn_link(a)) == op_Cond) {
742       /* Use the better Cond if the Proj projs from a Cond which get's
743          its result from an Eor/Not. */
744       assert (((get_irn_op(get_Cond_selector(a)) == op_Eor)
745                || (get_irn_op(get_Cond_selector(a)) == op_Not))
746               && (get_irn_mode(get_Cond_selector(a)) == mode_b)
747               && (get_irn_op(get_irn_link(a)) == op_Cond)
748               && (get_Cond_selector(get_irn_link(a)) == get_Eor_left(get_Cond_selector(a))));
749       set_Proj_pred(n, get_irn_link(a));
750       if (get_Proj_proj(n) == 0)
751         set_Proj_proj(n, 1);
752       else
753         set_Proj_proj(n, 0);
754     } else if ((get_irn_op(a) == op_Cond)
755                && (get_irn_mode(get_Cond_selector(a)) == mode_Iu)
756                && value_of(a)
757                && (get_Cond_kind(a) == dense)
758                && (get_opt_unreachable_code())) {
759       /* The Cond is a Switch on a Constant */
760       if (get_Proj_proj(n) == tv_val_uInt(value_of(a))) {
761         /* The always taken branch, reuse the existing Jmp. */
762         if (!get_irn_link(a)) /* well, if it exists ;-> */
763           set_irn_link(a, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
764         assert(get_irn_op(get_irn_link(a)) == op_Jmp);
765         n = get_irn_link(a);
766       } else {/* Not taken control flow, but be careful with the default! */
767         if (get_Proj_proj(n) < a->attr.c.default_proj){
768           /* a never taken branch */
769           n = new_Bad();
770         } else {
771           a->attr.c.default_proj = get_Proj_proj(n);
772         }
773       }
774     }
775   } break;
776   case iro_Eor: { /* @@@ not tested as boolean Eor not allowed any more. */
777     a = get_Eor_left(n);
778     b = get_Eor_right(n);
779
780     if ((get_irn_mode(n) == mode_b)
781         && (get_irn_op(a) == op_Proj)
782         && (get_irn_mode(a) == mode_b)
783         && (tarval_classify (computed_value (b)) == 1)
784         && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
785       /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
786       n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
787                      mode_b, get_negated_pnc(get_Proj_proj(a)));
788     else if ((get_irn_mode(n) == mode_b)
789              && (tarval_classify (computed_value (b)) == 1))
790       /* The Eor is a Not. Replace it by a Not. */
791       /*   ????!!!Extend to bitfield 1111111. */
792       n = new_r_Not(current_ir_graph, get_nodes_Block(n), a, mode_b);
793   }
794   break;
795   case iro_Not: {
796     a = get_Not_op(n);
797
798     if (   (get_irn_mode(n) == mode_b)
799         && (get_irn_op(a) == op_Proj)
800         && (get_irn_mode(a) == mode_b)
801         && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
802       /* We negate a Cmp. The Cmp has the negated result anyways! */
803       n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
804                      mode_b, get_negated_pnc(get_Proj_proj(a)));
805   }
806   break;
807   default: ;
808   }
809   return n;
810 }
811
812 /* **************** Common Subexpression Elimination **************** */
813
814 /* Compare function for two nodes in the hash table.   Gets two       */
815 /* nodes as parameters.  Returns 0 if the nodes are a cse.            */
816 static int
817 vt_cmp (const void *elt, const void *key)
818 {
819   ir_node *a, *b;
820   int i;
821
822   a = (void *)elt;
823   b = (void *)key;
824
825   if (a == b) return 0;
826
827   if ((get_irn_op(a) != get_irn_op(b)) ||
828       (get_irn_mode(a) != get_irn_mode(b))) return 1;
829
830   /* compare if a's in and b's in are equal */
831   if (get_irn_arity (a) != get_irn_arity(b))
832     return 1;
833
834   /* for block-local cse and pinned nodes: */
835   if (!get_opt_global_cse() || (get_op_pinned(get_irn_op(a)) == pinned)) {
836     if (get_irn_n(a, -1) != get_irn_n(b, -1))
837       return 1;
838   }
839
840   /* compare a->in[0..ins] with b->in[0..ins] */
841   for (i = 0; i < get_irn_arity(a); i++)
842     if (get_irn_n(a, i) != get_irn_n(b, i))
843       return 1;
844
845   switch (get_irn_opcode(a)) {
846   case iro_Const:
847     return get_irn_const_attr (a) != get_irn_const_attr (b);
848   case iro_Proj:
849     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
850   case iro_Filter:
851     return get_Filter_proj(a) != get_Filter_proj(b);
852   case iro_Alloc:
853     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
854       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
855   case iro_Free:
856     return (get_irn_free_attr(a) != get_irn_free_attr(b));
857   case iro_SymConst:
858     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
859       || (get_irn_symconst_attr(a).tori.typ != get_irn_symconst_attr(b).tori.typ);
860   case iro_Call:
861     return (get_irn_call_attr(a) != get_irn_call_attr(b));
862   case iro_Sel:
863     return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
864       || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
865       || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
866       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
867       || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
868   case iro_Phi:
869     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
870   default: ;
871   }
872
873   return 0;
874 }
875
876 static unsigned
877 ir_node_hash (ir_node *node)
878 {
879   unsigned h;
880   int i;
881
882   /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
883   h = get_irn_arity(node);
884
885   /* consider all in nodes... except the block. */
886   for (i = 0;  i < get_irn_arity(node);  i++) {
887     h = 9*h + (unsigned long)get_irn_n(node, i);
888   }
889
890   /* ...mode,... */
891   h = 9*h + (unsigned long) get_irn_mode (node);
892   /* ...and code */
893   h = 9*h + (unsigned long) get_irn_op (node);
894
895   return h;
896 }
897
898 pset *
899 new_identities (void)
900 {
901   return new_pset (vt_cmp, TUNE_NIR_NODES);
902 }
903
904 void
905 del_identities (pset *value_table)
906 {
907   del_pset (value_table);
908 }
909
910 /* Return the canonical node computing the same value as n.
911    Looks up the node in a hash table. */
912 static INLINE ir_node *
913 identify (pset *value_table, ir_node *n)
914 {
915   ir_node *o = NULL;
916
917   if (!value_table) return n;
918
919   if (get_opt_reassociation()) {
920     switch (get_irn_opcode (n)) {
921     case iro_Add:
922     case iro_Mul:
923     case iro_Or:
924     case iro_And:
925     case iro_Eor:
926       {
927         /* for commutative operators perform  a OP b == b OP a */
928         if (get_binop_left(n) > get_binop_right(n)) {
929           ir_node *h = get_binop_left(n);
930           set_binop_left(n, get_binop_right(n));
931           set_binop_right(n, h);
932         }
933       }
934       break;
935     default: break;
936     }
937   }
938
939   o = pset_find (value_table, n, ir_node_hash (n));
940   if (!o) return n;
941
942   return o;
943 }
944
945 /* During construction we set the pinned flag in the graph right when the
946    optimizatin is performed.  The flag turning on procedure global cse could
947    be changed between two allocations.  This way we are safe. */
948 static INLINE ir_node *
949 identify_cons (pset *value_table, ir_node *n) {
950   ir_node *old = n;
951   n = identify(value_table, n);
952   if (get_irn_n(old, -1) != get_irn_n(n, -1))
953     set_irg_pinned(current_ir_graph, floats);
954   return n;
955 }
956
957 /* Return the canonical node computing the same value as n.
958    Looks up the node in a hash table, enters it in the table
959    if it isn't there yet. */
960 static ir_node *
961 identify_remember (pset *value_table, ir_node *node)
962 {
963   ir_node *o = NULL;
964
965   if (!value_table) return node;
966
967   /* lookup or insert in hash table with given hash key. */
968   o = pset_insert (value_table, node, ir_node_hash (node));
969
970   if (o == node) return node;
971
972   return o;
973 }
974
975 void
976 add_identities (pset *value_table, ir_node *node) {
977   identify_remember (value_table, node);
978 }
979
980 /* garbage in, garbage out. If a node has a dead input, i.e., the
981    Bad node is input to the node, return the Bad node.  */
982 static INLINE ir_node *
983 gigo (ir_node *node)
984 {
985   int i;
986   ir_op* op = get_irn_op(node);
987
988   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
989      blocks predecessors is dead. */
990   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
991     for (i = -1; i < get_irn_arity(node); i++) {
992       if (is_Bad(get_irn_n(node, i))) {
993         return new_Bad();
994       }
995     }
996   }
997 #if 0
998   /* If Block has only Bads as predecessors it's garbage. */
999   /* If Phi has only Bads as predecessors it's garbage. */
1000   if (op == op_Block || op == op_Phi)  {
1001     for (i = 0; i < get_irn_arity(node); i++) {
1002       if (!is_Bad(get_irn_n(node, i))) break;
1003     }
1004     if (i = get_irn_arity(node)) node = new_Bad();
1005   }
1006 #endif
1007   return node;
1008 }
1009
1010
1011 /* These optimizations deallocate nodes from the obstack.
1012    It can only be called if it is guaranteed that no other nodes
1013    reference this one, i.e., right after construction of a node.  */
1014 ir_node *
1015 optimize (ir_node *n)
1016 {
1017   tarval *tv;
1018   ir_node *old_n = n;
1019
1020   /* Allways optimize Phi nodes: part of the construction. */
1021   if ((!get_optimize()) && (get_irn_op(n) != op_Phi)) return n;
1022
1023   /* constant expression evaluation / constant folding */
1024   if (get_opt_constant_folding()) {
1025     /* constants can not be evaluated */
1026     if  (get_irn_op(n) != op_Const) {
1027       /* try to evaluate */
1028       tv = computed_value (n);
1029       if ((get_irn_mode(n) != mode_T) && (tv != NULL)) {
1030         /* evaluation was succesful -- replace the node. */
1031         obstack_free (current_ir_graph->obst, n);
1032         return new_Const (get_tv_mode (tv), tv);
1033       }
1034     }
1035   }
1036
1037   /* remove unnecessary nodes */
1038   if (get_opt_constant_folding() ||
1039       (get_irn_op(n) == op_Phi)  ||   /* always optimize these nodes. */
1040       (get_irn_op(n) == op_Id)   ||
1041       (get_irn_op(n) == op_Proj) ||
1042       (get_irn_op(n) == op_Block)  )  /* Flags tested local. */
1043     n = equivalent_node (n);
1044
1045   /** common subexpression elimination **/
1046   /* Checks whether n is already available. */
1047   /* The block input is used to distinguish different subexpressions. Right
1048      now all nodes are pinned to blocks, i.e., the cse only finds common
1049      subexpressions within a block. */
1050   if (get_opt_cse())
1051     n = identify_cons (current_ir_graph->value_table, n);
1052
1053   if (n != old_n) {
1054     /* We found an existing, better node, so we can deallocate the old node. */
1055     obstack_free (current_ir_graph->obst, old_n);
1056   }
1057
1058   /* Some more constant expression evaluation that does not allow to
1059      free the node. */
1060   if (get_opt_constant_folding() ||
1061       (get_irn_op(n) == op_Cond) ||
1062       (get_irn_op(n) == op_Proj))     /* Flags tested local. */
1063     n = transform_node (n);
1064
1065   /* Remove nodes with dead (Bad) input.
1066      Run always for transformation induced Bads. */
1067   n = gigo (n);
1068
1069   /* Now we can verify the node, as it has no dead inputs any more. */
1070   irn_vrfy(n);
1071
1072   /* Now we have a legal, useful node. Enter it in hash table for cse */
1073   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
1074     n = identify_remember (current_ir_graph->value_table, n);
1075   }
1076
1077   return n;
1078 }
1079
1080
1081 /* These optimizations never deallocate nodes.  This can cause dead
1082    nodes lying on the obstack.  Remove these by a dead node elimination,
1083    i.e., a copying garbage collection. */
1084 ir_node *
1085 optimize_in_place_2 (ir_node *n)
1086 {
1087   tarval *tv;
1088   ir_node *old_n = n;
1089
1090   if (!get_optimize() && (get_irn_op(n) != op_Phi)) return n;
1091
1092   /* if not optimize return n */
1093   if (n == NULL) {
1094     assert(0);
1095     /* Here this is possible.  Why? */
1096     return n;
1097   }
1098
1099
1100   /* constant expression evaluation / constant folding */
1101   if (get_opt_constant_folding()) {
1102     /* constants can not be evaluated */
1103     if  (get_irn_op(n) != op_Const) {
1104       /* try to evaluate */
1105       tv = computed_value (n);
1106       if ((get_irn_mode(n) != mode_T) && (tv != NULL)) {
1107         /* evaluation was succesful -- replace the node. */
1108         n = new_Const (get_tv_mode (tv), tv);
1109         __dbg_info_merge_pair(n, old_n, dbg_const_eval);
1110         return n;
1111       }
1112     }
1113   }
1114
1115   /* remove unnecessary nodes */
1116   /*if (get_opt_constant_folding()) */
1117   if (get_opt_constant_folding() ||
1118       (get_irn_op(n) == op_Phi)  ||   /* always optimize these nodes. */
1119       (get_irn_op(n) == op_Id)   ||   /* ... */
1120       (get_irn_op(n) == op_Proj) ||   /* ... */
1121       (get_irn_op(n) == op_Block)  )  /* Flags tested local. */
1122     n = equivalent_node (n);
1123
1124   /** common subexpression elimination **/
1125   /* Checks whether n is already available. */
1126   /* The block input is used to distinguish different subexpressions.  Right
1127      now all nodes are pinned to blocks, i.e., the cse only finds common
1128      subexpressions within a block. */
1129   if (get_opt_cse()) {
1130     n = identify (current_ir_graph->value_table, n);
1131   }
1132
1133   /* Some more constant expression evaluation. */
1134   if (get_opt_constant_folding() ||
1135       (get_irn_op(n) == op_Cond) ||
1136       (get_irn_op(n) == op_Proj))     /* Flags tested local. */
1137     n = transform_node (n);
1138
1139   /* Remove nodes with dead (Bad) input.
1140      Run always for transformation induced Bads.  */
1141   n = gigo (n);
1142
1143   /* Now we can verify the node, as it has no dead inputs any more. */
1144   irn_vrfy(n);
1145
1146   /* Now we have a legal, useful node. Enter it in hash table for cse.
1147      Blocks should be unique anyways.  (Except the successor of start:
1148      is cse with the start block!) */
1149   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
1150     n = identify_remember (current_ir_graph->value_table, n);
1151
1152   return n;
1153 }
1154
1155 /* Wrapper for external use, set proper status bits after optimization */
1156 ir_node *
1157 optimize_in_place (ir_node *n) {
1158   /* Handle graph state */
1159   assert(get_irg_phase_state(current_ir_graph) != phase_building);
1160   if (get_opt_global_cse())
1161     set_irg_pinned(current_ir_graph, floats);
1162   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1163     set_irg_outs_inconsistent(current_ir_graph);
1164   /* Maybe we could also test whether optimizing the node can
1165      change the control graph. */
1166   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1167     set_irg_dom_inconsistent(current_ir_graph);
1168   return optimize_in_place_2 (n);
1169 }