BugFix:
[libfirm] / ir / opt / data_flow_scalar_replace.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   scalar replacement of arrays and compounds
23  * @author  Beyhan Veliev, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "iroptimize.h"
29
30 #include <string.h>
31
32 #include "irflag_t.h"
33 #include "irouts.h"
34 #include "pset.h"
35 #include "ircons_t.h"
36 #include "hashptr.h"
37 #include "irgwalk.h"
38 #include "irnode_t.h"
39 #include "irtools.h"
40 #include "irdump.h"
41 #include "irloop.h"
42 #include "analyze_irg_args.h"
43 #include "irprintf.h"
44 #include "irgopt.h"
45 #include "xmalloc.h"
46
47 #define SET_ENT_VNUM(ent, vnum) set_entity_link(ent, INT_TO_PTR(vnum))
48 #define GET_ENT_VNUM(ent)       (unsigned)PTR_TO_INT(get_entity_link(ent))
49 #define SET_IRN_VNUM(irn, vnum) set_irn_link(irn, INT_TO_PTR(vnum))
50 #define GET_IRN_VNUM(irn)       (unsigned)PTR_TO_INT(get_irn_link(irn))
51 #define SYNCED    8
52
53
54 typedef struct _ent_leaves_t{
55   ir_entity *ent;             /**< An entity, that contains scalars for replace.*/
56   pset *leaves;               /**< All leaves of this entity.*/
57 } ent_leaves_t;
58
59 typedef struct _sels_t {
60   ir_node *sel;               /**< A sel node, thats entity have scalars.*/
61   ir_entity  *ent;            /**< The entity of this sel node.*/
62 }sels_t;
63
64 typedef struct _call_access_t {
65   ir_node *call;             /**< A call node, that have as parameter a scalar.*/
66   unsigned int access_type;  /**< The access type, with that this call access this scalar.*/
67 }call_access_t;
68
69 typedef struct _fixlist_entry_t {
70   ir_node *irn;             /**< An ir node, that must be fixed.*/
71   unsigned int vnum;        /**< The value number, that must became this ir node.*/
72 }fixlist_entry_t;
73
74 typedef struct _syncs_fixlist_entry_t {
75   ir_node *irn;             /**< A sync node that must be fixed.*/
76   int *accessed_vnum;       /**< A pointer to save an array with value numbers, that must became this sync.*/
77 }syncs_fixlist_entry_t;
78
79 /* A entry, that save the memory
80  * edge state and the access state for this leave
81  * int the array,that is created for every block.*/
82 typedef struct _leave_t {
83   ir_node *mem_edge_state;   /**< memory state for this scalar in this block.*/
84   unsigned int access_type;  /**< access state for this scalar in this block.*/
85   set *calls;                /**< call nodes,that change this scalar in this block.*/
86 }value_arr_entry_t;
87
88 /**
89  * A path element entry: it is either an entity
90  * or a tarval, because we evaluate only constant array
91  * accesses like a.b.c[8].d
92  */
93 typedef union {
94   ir_entity *ent;
95   tarval *tv;
96 } path_elem_t;
97
98 /**
99  * An access path, used to assign value numbers
100  * to variables that will be scalar replaced
101  */
102 typedef struct _path_t {
103   unsigned    vnum;      /**< the value number */
104   unsigned    path_len;  /**< the length of the access path */
105   path_elem_t path[1];   /**< the path */
106 } path_t;
107
108 /**
109  * environment for memory walker
110  */
111 typedef struct _env_t {
112   struct obstack obst;                   /**< a obstack for the memory edge */
113   set                   *set_sels;       /**< a set with all sels, that are reachable from an entity with a scalar.*/
114   set                   *set_ent;        /**< a set with all entities that have one or more scalars.*/
115   fixlist_entry_t       *fix_phis;       /**< list of all Phi nodes that must be fixed */
116   fixlist_entry_t       *fix_ls;         /**< list of all Load or Store nodes that must be fixed */
117   syncs_fixlist_entry_t *fix_syncs;      /**< list of all Sync nodes that must be fixed */
118   unsigned int          nvals;           /**< to save the number of scalars.*/
119   unsigned int          gl_mem_vnum;     /**< indicate the position of the globule memory edge state in var_arr.*/
120   unsigned int          vnum_state;      /**< indicate the position of the value number state in var_arr.*/
121   unsigned int          changes;         /**< to save if by anlyse_calls is changed anything.*/
122 } env_t;
123
124
125
126 /**
127  * Compare two elements of the ent_leaves_t set.
128  *
129  * @return 0 if they are identically
130  */
131 static int ent_leaves_t_cmp(const void *elt, const void *key, size_t size)
132 {
133   const ent_leaves_t *c1 = elt;
134   const ent_leaves_t *c2 = key;
135   (void) size;
136
137   return c1->ent != c2->ent;
138 }
139
140 /**
141  * Compare two elements of the ent_access_t set.
142  *
143  * @return 0 if they are identically
144  */
145 static int ent_cmp(const void *elt, const void *key)
146 {
147   const ir_entity *c1 = elt;
148   const ir_entity *c2 = key;
149
150   return c1 != c2;
151 }
152
153 /**
154  * Compare two elements of the sels_t set.
155  *
156  * @return 0 if they are identically
157  */
158 static int sels_cmp(const void *elt, const void *key, size_t size)
159 {
160   const sels_t *c1 = elt;
161   const sels_t *c2 = key;
162   (void) size;
163
164   return c1->sel != c2->sel;
165 }
166
167 /**
168  * Compare two elements of the leave_t set.
169  *
170  * @return 0 if they are identically
171  */
172 static int leave_cmp(const void *elt, const void *key)
173 {
174   ir_node *c1 = (ir_node *)elt;
175   ir_node *c2 = (ir_node *)key;
176
177   return get_Sel_entity(c1) != get_Sel_entity(c2);
178 }
179
180 /**
181  * Compare two elements of the call_access_t set.
182  *
183  * @return 0 if they are identically
184  */
185 static int call_cmp(const void *elt, const void *key, size_t size)
186 {
187   const call_access_t *c1 = elt;
188   const call_access_t *c2 = key;
189   (void) size;
190
191   return c1->call != c2->call;
192 }
193
194 /**
195  * Compare two paths.
196  *
197  * @return 0 if they are identically
198  */
199 static int path_cmp(const void *elt, const void *key, size_t size)
200 {
201   const path_t *p1 = elt;
202   const path_t *p2 = key;
203   (void) size;
204
205   /* we can use memcmp here, because identical tarvals should have identical addresses */
206   return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
207 }
208
209 /**
210  * Calculate a hash value for a path.
211  */
212 static unsigned path_hash(const path_t *path)
213 {
214   unsigned hash = 0;
215   unsigned i;
216
217   for (i = 0; i < path->path_len; ++i)
218     hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
219
220   return hash >> 4;
221 }
222
223 /**
224  * Returns non-zero, if all induces of a Sel node are constants.
225  *
226  * @param sel  the Sel node that will be checked
227  */
228 static int is_const_sel(ir_node *sel) {
229   int i, n = get_Sel_n_indexs(sel);
230
231   for (i = 0; i < n; ++i) {
232     ir_node *idx = get_Sel_index(sel, i);
233
234     if (!is_Const(idx))
235       return 0;
236   }
237   return 1;
238 }
239
240 /**
241  * Returns non-zero, if the address of an entity
242  * represented by a Sel node (or it's successor Sels) is taken.
243  */
244 static int is_address_taken_2(ir_node *sel)
245 {
246   int i;
247
248   if (! is_const_sel(sel))
249     return 1;
250
251   for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
252     ir_node *succ = get_irn_out(sel, i);
253
254     switch (get_irn_opcode(succ)) {
255     case iro_Load:
256       /* ok, we just load from that entity */
257       break;
258
259     case iro_Store:
260       /* check that Sel is not the Store's value */
261       if (get_Store_value(succ) == sel)
262         return 1;
263       break;
264
265     case iro_Sel: {
266       /* Check the Sel successor of Sel */
267       int res = is_address_taken_2(succ);
268
269       if (res)
270         return 1;
271       break;
272     }
273
274     case iro_Call:
275       /* The address of an entity is given as a parameter.
276        * We analyzes that later and optimizes this scalar
277        * if possible.
278        */
279       return 0;
280
281     default:
282       /* another op, the address is taken */
283       return 1;
284     }
285   }
286   return 0;
287 }
288
289 /**
290  * Link all Sels with the entity.
291  *
292  * @param ent  the entity that will be scalar replaced
293  * @param sel  a Sel node that selects some fields of this entity
294  */
295 static void link_all_leave_sels(ir_entity *ent, ir_node *sel)
296 {
297   int i, n;
298
299   n = get_irn_n_outs(sel);
300   for (i = 0; i < n; ++i) {
301     ir_node *succ = get_irn_out(sel, i);
302
303     if (is_Sel(succ))
304       link_all_leave_sels(ent, succ);
305   }
306
307    /* if Sel nodes with memory inputs are used, a entity can be
308     * visited more than once causing a ring here, so we use the
309     * node flag to mark linked nodes
310     */
311    if (irn_visited_else_mark(sel))
312     return;
313
314   /*
315    * we link the sels to the entity.
316    */
317   set_irn_link(sel, get_entity_link(ent));
318   set_entity_link(ent, sel);
319 }
320
321 /* we need a special address that serves as an address taken marker */
322 static char _x;
323 static void *ADDRESS_TAKEN = &_x;
324
325 /**
326  * Find possible scalar replacements.
327  *
328  * @param irg  an IR graph
329  *
330  * This function finds variables on the (members of the) frame type
331  * that can be scalar replaced, because their address is never taken.
332  * If such a variable is found, it's entity link will hold a list of all
333  * Sel nodes, that selects anythings of this entity.
334  * Otherwise, the link will be ADDRESS_TAKEN or NULL.
335  *
336  * @return  non-zero if at least one entity could be replaced
337  *          potentially
338  */
339 static int find_possible_replacements(ir_graph *irg)
340 {
341   ir_node *irg_frame = get_irg_frame(irg);
342   ir_type *frame_tp;
343   int i, n;
344   int res = 0;
345
346   inc_irg_visited(irg);
347
348   n = get_irn_n_outs(irg_frame);
349
350   /*
351    * First, clear the link field of all interesting entities.
352    */
353   frame_tp = get_irg_frame_type(irg);
354   for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
355     ir_entity *ent = get_class_member(frame_tp, i);
356     set_entity_link(ent, NULL);
357   }
358
359   /*
360    * Check the ir_graph for Sel nodes. If the entity of Sel
361    * isn't a scalar replacement set the link of this entity
362    * equal ADDRESS_TAKEN.
363    */
364   for (i = 0; i < n; ++i) {
365     ir_node *succ = get_irn_out(irg_frame, i);
366
367     if (is_Sel(succ)) {
368       ir_entity *ent = get_Sel_entity(succ);
369       ir_type *ent_type;
370
371       /* we are only interested in entities on the frame, NOT
372          on the value type */
373       if (get_entity_owner(ent) != frame_tp)
374         continue;
375
376      if (get_entity_link(ent) == ADDRESS_TAKEN)
377         continue;
378
379       /*
380        * Beware: in rare cases even entities on the frame might be
381        * volatile. This might happen if the entity serves as a store
382        * to a value that must survive a exception. Do not optimize
383        * such entities away.
384        */
385       if (get_entity_volatility(ent) == volatility_is_volatile) {
386         set_entity_link(ent, ADDRESS_TAKEN);
387         continue;
388       }
389
390       ent_type = get_entity_type(ent);
391
392       /* we can handle arrays, structs and atomic types yet */
393       if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
394         if (is_address_taken_2(succ)) {
395           if (get_entity_link(ent)) /* killing one */
396             --res;
397           set_entity_link(ent, ADDRESS_TAKEN);
398         }
399         else {
400           /* possible found one */
401           if (get_entity_link(ent) == NULL)
402             ++res;
403           link_all_leave_sels(ent, succ);
404         }
405       }
406     }
407   }
408
409   return res;
410 }
411
412 static int is_leave_sel(ir_node *sel) {
413   int i;
414   ir_node *succ;
415
416   for(i = get_irn_n_outs(sel) - 1; i >= 0; i--) {
417     succ = get_irn_out(sel, i);
418     if (is_Sel(succ))
419       return 0;
420   }
421
422   return 1;
423 }
424
425 /**
426  * Return a path from the Sel node sel to it's root.
427  *
428  * @param sel  the Sel node
429  * @param len  the length of the path so far
430  */
431 static path_t *find_path(ir_node *sel, unsigned len)
432 {
433   int pos, i, n;
434   path_t *res;
435   ir_node *pred = get_Sel_ptr(sel);
436
437   /* the current Sel node will add some path elements */
438   n    = get_Sel_n_indexs(sel);
439   len += n + 1;
440
441   if (!is_Sel(pred)) {
442     /* we found the root */
443     res = XMALLOCF(path_t, path, len);
444     res->path_len = len;
445   }
446   else
447     res = find_path(pred, len);
448
449   pos = res->path_len - len;
450
451   res->path[pos++].ent = get_Sel_entity(sel);
452   for (i = 0; i < n; ++i) {
453     ir_node *index = get_Sel_index(sel, i);
454
455     if (is_Const(index))
456       res->path[pos++].tv = get_Const_tarval(index);
457   }
458   return res;
459 }
460
461 /**
462  * Allocate value numbers for the leaves
463  * in our found entities.
464  *
465  * @param sels  a set that will contain all Sels that have a value number
466  * @param ent   the entity that will be scalar replaced
467  * @param vnum  the first value number we can assign
468  * @param modes a flexible array, containing all the modes of
469  *              the value numbers.
470  *
471  * @return the next free value number
472  */
473 static unsigned allocate_value_numbers(set *set_sels, pset *leaves, ir_entity *ent, unsigned vnum)
474 {
475   ir_node *sel, *next;
476   path_t *key, *path;
477   sels_t       key_sels;
478   set *pathes = new_set(path_cmp, 8);
479
480   /* visit all Sel nodes in the chain of the entity */
481   for (sel = get_entity_link(ent); sel; sel = next) {
482     next = get_irn_link(sel);
483
484     /* we save for every sel it root entity, why
485      * we need this information, when we split the memory edge,
486      * and we must mark this sel for later. */
487      key_sels.ent = ent;
488      key_sels.sel = sel;
489      set_insert(set_sels, &key_sels, sizeof(key_sels), HASH_PTR(sel));
490
491     if(! is_leave_sel(sel))
492       continue;
493     /* We have found a leave and we add it to the pset of this entity.*/
494     pset_insert(leaves, sel, HASH_PTR(get_Sel_entity(sel)));
495
496     key  = find_path(sel, 0);
497     path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
498
499     if (path)
500       SET_IRN_VNUM(sel, path->vnum);
501     else {
502
503       key->vnum = vnum++;
504
505       set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
506
507       SET_IRN_VNUM(sel, key->vnum);
508     }
509     free(key);
510   }
511
512   del_set(pathes);
513   set_entity_link(ent, NULL);
514   return vnum;
515 }
516 /**
517  * Add a sync node to it fix list.
518  *
519  * @param sync     The sync node, that myst be addet to the fix list.
520  * @param unk_vnum An array whit the value number, that are synced with this sync node.
521  * @param env      The enviroment pinter.
522  */
523 static void add_sync_to_fixlist(ir_node *sync, int *unk_vnum, env_t *env) {
524
525    syncs_fixlist_entry_t *s;
526
527    s = obstack_alloc(&env->obst, sizeof(*s));
528    s->irn  = sync;
529    s->accessed_vnum = unk_vnum;
530    set_irn_link(sync, env->fix_syncs);
531    env->fix_syncs = s;
532 }
533 /**
534  * Add a ir node to it fix list.
535  *
536  * @param irn     The ir node, that myst be addet to the fix list.
537  * @param vnum    The value number, that must baceme this ir node as predecessor later.
538  * @param env     The enviroment pinter.
539  */
540 static void add_ls_to_fixlist(ir_node *irn, int vnum, env_t *env) {
541
542   fixlist_entry_t *l;
543
544   l = obstack_alloc(&env->obst, sizeof(*l));
545   l->irn  = irn;
546   l->vnum = vnum;
547
548   if(get_irn_op(irn) == op_Phi) {
549     set_irn_link(l->irn, env->fix_phis);
550     env->fix_phis = l;
551   }else {
552     set_irn_link(l->irn, env->fix_ls);
553     env->fix_ls = l;
554   }
555 }
556
557 static void add_mem_edge(value_arr_entry_t *val_arr, int vnum, ir_node ***in, int **accessed_vnum) {
558
559   if(val_arr[vnum].mem_edge_state != NULL)
560     ARR_APP1(ir_node *, *in, val_arr[vnum].mem_edge_state);
561   else {
562     ARR_APP1(int, *accessed_vnum, vnum);
563     ARR_APP1(ir_node *, *in, new_Unknown(mode_M));
564   }
565 }
566 /**
567  * The function handles the scalars, that wase stored
568  * in this block.
569  *
570  * @param blk    The block, that must be handled.
571  * @param env    The enviroment pinter.
572  */
573
574 /* Return the memory successor of the call node.*/
575 static ir_node *get_Call_mem_out(ir_node *call) {
576
577   int i;
578   ir_node *mem;
579
580   for(i = get_irn_n_outs(call) - 1; i >= 0; i--) {
581     mem = get_irn_out(call, i);
582     if(get_irn_mode(mem) == mode_M)
583       return mem;
584   }
585   /* is not reachable*/
586   return NULL;
587 }
588
589
590 static void sync_stored_scalars(ir_node *blk, env_t *env) {
591
592   int                   i;
593   int                   *unk_vnum;                   /**< An arraw, where are saved the value number, that
594                                                           are synced from this sync node.*/
595   ent_leaves_t          *value_ent;
596   value_arr_entry_t     *val_arr_blk, *val_arr;
597   ir_node               *pred, *leave, *sync, **in;
598   ir_node               *sync_blk;                     /**< The block, where the sync node must be created.*/
599
600
601   val_arr_blk = get_irn_link(blk);
602
603   for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent)) {
604
605
606     if(val_arr_blk[GET_ENT_VNUM(value_ent->ent)].access_type <= 3)
607       /* This entity is not stored in this block.*/
608       continue;
609
610     for(i = get_Block_n_cfgpreds(blk) - 1; i >= 0; i--) {
611
612       pred = get_Block_cfgpred(blk, i);
613       pred = get_nodes_block(pred);
614       val_arr = get_irn_link(pred);
615
616       if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type == SYNCED)
617         /* This entity was synced.*/
618         continue;
619
620       if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type <= 3) {
621
622         /* To avoid repeated sync of this entity in this block.*/
623         val_arr[GET_ENT_VNUM(value_ent->ent)].access_type = SYNCED;
624         /* In this predecessor block is this entity not acessed.
625          * We must sync in the end ot this block.*/
626         if(get_Block_n_cfgpreds(blk) > 1)
627           sync_blk = get_nodes_block(get_Block_cfgpred(blk, i));
628         else
629           sync_blk = blk;
630
631         val_arr = get_irn_link(sync_blk);
632         /* An array to save the memory edges, that must be
633          * synced.*/
634         in = NEW_ARR_F(ir_node *, 1);
635
636         /* An array to save the value numbers,
637          * that must be repaired.*/
638         unk_vnum = NEW_ARR_F(int, 0);
639         /* The global memory edge.*/
640         if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL)
641          in[0] = new_Unknown(mode_M);
642         else
643          in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
644
645         for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves))
646           /* All this memory edges must be synced.*/
647           add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &unk_vnum);
648
649         /* We create the sync and set it in the global memory state.*/
650         sync = new_r_Sync(current_ir_graph, sync_blk, ARR_LEN(in), in);
651         /* We must check this, why it is possible to get a Bad node
652          * form new_r_Sync(), when the node can be optimized.
653          * In this case we must do nothing.*/
654         if (is_Sync(sync))  {
655           val_arr[env->gl_mem_vnum].mem_edge_state = sync;
656           /* We add this sync node to the sync's fix list.*/
657           add_sync_to_fixlist(val_arr[env->gl_mem_vnum].mem_edge_state, unk_vnum, env);
658         }
659         DEL_ARR_F(in);
660       }
661     }
662   }
663 }
664 /**
665  * The function split the memory edge of load and store nodes, that have
666  * as predecessor a scalar
667  *
668  * @param irn   The node, that memory edge must be spleted.
669  * @param env   The enviroment pinter.
670  */
671 static void split_ls_mem_edge(ir_node *irn, env_t *env) {
672
673   ir_op              *op;
674   ir_node            *leave, *irn_blk, *mem_state, *new_mem_state;
675   unsigned           ent_vnum, sel_vnum, i;
676   value_arr_entry_t  *val_arr;
677   sels_t             key_sels, *value_sels;
678   ent_leaves_t       key_ent, *value_ent;
679
680   op = get_irn_op(irn);
681
682   if(op == op_Load)
683     key_sels.sel = get_Load_ptr(irn);
684   else
685     key_sels.sel = get_Store_ptr(irn);
686
687   value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
688
689   if(value_sels != NULL) {
690     /* we have found a load or store, that use a sel of our set
691      * and we must split or extend, if the memory edge have been
692      * split for this sel, the memory edge.*/
693
694     key_ent.ent = value_sels->ent;
695     value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
696     /*To check if the enities set is right filled. */
697     assert(value_ent && " This sel's entity isn't int the entity set.");
698
699     leave = pset_find(value_ent->leaves, key_sels.sel, HASH_PTR(get_Sel_entity(key_sels.sel)));
700     /*To check if the leaves set is right filled. */
701     assert(leave && "Anything in data_flow_scalar_replacment algorithm is wrong.");
702
703     ent_vnum = GET_ENT_VNUM(value_ent->ent);
704     sel_vnum = GET_IRN_VNUM(leave);
705     irn_blk = get_nodes_block(irn);
706     val_arr   = get_irn_link(irn_blk);
707
708     if(val_arr[ent_vnum].access_type == 0)
709       /* We have found a scalar, that address is not stored as jet.*/
710       i = sel_vnum;
711     else
712       /* This scalar have been stored.*/
713       i = env->gl_mem_vnum;
714
715     if(val_arr[i].mem_edge_state == NULL) {
716       /* We split now for this sel the memory edge in this block.*/
717       mem_state = new_Unknown(mode_M);
718       /* We must mark this node to fix later*/
719       add_ls_to_fixlist(irn, i, env);
720     }
721     else
722       /* We have split the memory edge and the current state is saved.*/
723       mem_state = val_arr[i].mem_edge_state;
724
725     /* We set this Load or Store to the memory edge of this
726      * sel.*/
727     if(op == op_Load)
728       set_Load_mem(irn, mem_state);
729     else
730       set_Store_mem(irn, mem_state);
731
732     /* When we have split or extended the memory edge we must
733      * update the memory_edge_state of this sel*/
734     new_mem_state = get_irn_out(irn, 0);
735     if(get_irn_mode(new_mem_state) == mode_M)
736       val_arr[i].mem_edge_state = new_mem_state;
737     else
738       val_arr[i].mem_edge_state = get_irn_out(irn, 1);
739   }
740 }
741
742 /**
743  * The function split the memory edge of phi nodes, that have
744  * as predecessor a scalar
745  *
746  * @param irn   The phi node, that memory edge must be spleted.
747  * @param env   The enviroment pinter.
748  */
749 static void split_phi_mem_edge(ir_node *irn, env_t *env) {
750
751   ir_node            *irn_blk, *unk, *leave, **in;
752   int                n, j;
753   ent_leaves_t       *value_ent;
754   value_arr_entry_t  *val_arr;
755
756   irn_blk = get_nodes_block(irn);
757   val_arr = get_irn_link(irn_blk);
758
759   n  = get_Block_n_cfgpreds(irn_blk);
760   in = ALLOCAN(ir_node*, n);
761
762   for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent))
763      if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type < 3)
764        /* This scalar wasn't be saved and we need to produce a phi for it.*/
765        for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
766
767          unk = new_Unknown(mode_M);
768          for (j = n - 1; j >= 0; --j)
769            in[j] = unk;
770
771          val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_r_Phi(current_ir_graph, irn_blk, n, in, mode_M);
772
773          add_ls_to_fixlist(val_arr[GET_IRN_VNUM(leave)].mem_edge_state, GET_IRN_VNUM(leave), env);
774        }
775
776   /* We use for the global memory the phi node, that
777    * is already available.*/
778   val_arr[env->gl_mem_vnum].mem_edge_state = irn;
779 }
780
781 /**
782  * The function handles the call nodes, that have
783  * as parameter a scalar
784  *
785  * @param env                The enviroment pinter.
786  * @param call               The call node, that must be handled.
787  * @param accessed_entities  A set wit all entities, that are accessed from this call node.*/
788 static void split_call_mem_edge(env_t *env, ir_node *call, pset *accessed_entities) {
789
790   ent_leaves_t            key_ent, *value_ent;
791   value_arr_entry_t       *val_arr;
792   call_access_t           key_call, *value_call;
793   ir_node                 *call_blk, *new_mem_state, *leave;
794   ir_node                 *sync, **in;
795   ir_entity               *ent;
796   unsigned                ent_vnum;
797   int                     fix_irn = 0;                  /**< Set to 1 if we must add this call to it fix list.*/
798   int                     *accessed_leaves_vnum = NULL; /**< An arraw, where are saved the value number, that
799                                                              are synced from call's sync node, if we need it.*/
800
801   call_blk = get_nodes_block(call);
802   val_arr  = get_irn_link(call_blk);
803   /* An array to save the memory edges, that must be
804    * synced.*/
805   in       = NEW_ARR_F(ir_node *, 1);
806   /* An array to save the value numbers of the memory
807    * edges that must be repaired.*/
808   accessed_leaves_vnum = NEW_ARR_F(int, 0);
809
810   /* We get the memory successor of the call node.
811    * It is the new memory state for all synced memory
812    * edges.*/
813   new_mem_state = get_Call_mem_out(call);
814
815   /* The global memory is the first predecessor of the create sync node.*/
816   if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
817     in[0] = new_Unknown(mode_M);
818     fix_irn = 1;
819   }
820   else
821     in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
822
823
824   for(ent = pset_first(accessed_entities); ent; ent = pset_next(accessed_entities)) {
825     /* Whit this loop we iterate all accessed entities from this call and collect
826      * all memory edges, that we must sync.*/
827     ent_vnum = GET_ENT_VNUM(ent);
828
829     key_call.call = call;
830     value_call    = set_find(val_arr[ent_vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
831
832     key_ent.ent   = ent;
833     value_ent     = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
834
835     if(val_arr[ent_vnum].access_type <= 3) {
836       /* This scalar's address wasn't stored in this block.*/
837       switch(value_call->access_type) {
838
839       case ptr_access_none :
840         /* In this case we have nothing to do.*/
841       break;
842
843       case ptr_access_read:
844       case ptr_access_write:
845       case ptr_access_rw:
846         /* All this cases must be traded equal.*/
847
848         for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
849           /* All this memory edges must be synced.*/
850           add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &accessed_leaves_vnum);
851
852           /* We update the memory state of this leave.*/
853           if(value_call->access_type != ptr_access_read)
854            val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_mem_state;
855         }
856
857       /* We are ready.*/
858       break;
859       }
860     }
861   }
862
863   /* We must update the global memory state.*/
864   val_arr[env->gl_mem_vnum].mem_edge_state = new_mem_state;
865
866   if(ARR_LEN(in) == 1) {
867     /* we must set the call memory to gobale momory*/
868     set_Call_mem(call,in[0]);
869
870     if(fix_irn)
871       /* We add this call node to the call fix list..*/
872       add_ls_to_fixlist(call, env->gl_mem_vnum, env);
873
874   } else {
875    /* We create the sync and set it as memory predecessor of the call node.*/
876       sync = new_r_Sync(current_ir_graph, call_blk, ARR_LEN(in), in);
877       /* We must check this, why it is possible to get a Bad node
878        * form new_r_Sync(), when the node can be optimized.
879        * In this case we must do nothing.*/
880       if (is_Sync(sync)) {
881         set_Call_mem(call, sync);
882         if(ARR_LEN(accessed_leaves_vnum))
883           /* We add this sync node to the sync's fix list.*/
884           add_sync_to_fixlist(sync, accessed_leaves_vnum, env);
885       }
886   }
887   DEL_ARR_F(in);
888 }
889
890 /**
891  * The function split the memory edge from the passed
892  * ir node if this is needed
893  *
894  * @param irn   The node, that memory edge must be spleted.
895  * @param env   The enviroment pinter.
896  */
897 static void split_memory_edge(ir_node *irn, void *ctx) {
898
899    env_t              *env = ctx;
900    ir_node            *sel, *irn_blk;
901    ir_op              *op;
902    sels_t             key_sels, *value_sels;
903    value_arr_entry_t  *val_arr;
904    pset               *accessed_entities;  /**< A set to save all entities accessed from a call.*/
905    int                i;
906
907
908    op = get_irn_op(irn);
909
910    if(op == op_Block)
911      irn_blk = irn;
912    else
913      irn_blk = get_nodes_block(irn);
914
915    if (!Block_block_visited(irn_blk)) {
916     /* We sync first the stored scalar address in this block.*/
917     mark_Block_block_visited(irn_blk);
918     sync_stored_scalars(irn_blk, env);
919    }
920
921    if(op == op_Load || op == op_Store)
922
923       split_ls_mem_edge(irn, env);
924
925    else {
926       if (op == op_Phi && get_irn_mode(irn) == mode_M) {
927         /*
928          * found a memory Phi: Here, we must create new Phi nodes
929          */
930         split_phi_mem_edge(irn, env);
931       }
932       else {
933         if(op == op_Call) {
934
935           /* Calls that have a NoMem input do neither read nor write memory.
936              We can completely ignore them here. */
937           if (is_NoMem(get_Call_mem(irn)))
938             return;
939
940           /* We save in this set all entities,
941            * that are accessed from this call node.*/
942           accessed_entities = new_pset(ent_cmp, 8);
943           val_arr = get_irn_link(get_nodes_block(irn));
944
945           for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
946
947             sel = get_Call_param(irn, i);
948             value_sels = NULL;
949             if (is_Sel(sel)) {
950               key_sels.sel = sel;
951               value_sels   = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
952
953             if(value_sels != NULL && val_arr[GET_ENT_VNUM(value_sels->ent)].access_type <= 3)
954               /* We save in this set all accessed entities from this call node whit
955                * access none, read, write or rw..*/
956               pset_insert(accessed_entities, value_sels->ent, HASH_PTR(value_sels->ent));
957             }
958           }
959
960           if(pset_count(accessed_entities))
961              split_call_mem_edge(env, irn, accessed_entities);
962
963           del_pset(accessed_entities);
964         }
965       }
966    }
967 }
968
969 /**
970  * searches through blocks beginning from block for value
971  * vnum and return it.
972  *
973  * @param block A block from the current ir graph.
974  * @param vnum  The value number, that must be found.
975  */
976 static ir_node *find_vnum_value(ir_node *block, unsigned vnum)
977 {
978   value_arr_entry_t *val_arr;
979   int               i;
980   ir_node           *res;
981
982   if (!Block_block_visited(block)) {
983     mark_Block_block_visited(block);
984
985     val_arr = get_irn_link(block);
986
987     if (val_arr[vnum].mem_edge_state)
988       return val_arr[vnum].mem_edge_state;
989
990     for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
991       ir_node *pred = get_Block_cfgpred(block, i);
992
993       res = find_vnum_value(get_nodes_block(pred), vnum);
994       if (res)
995         return res;
996     }
997   }
998   return NULL;
999 }
1000
1001 /**
1002  * fix the Load/Store or Call list
1003  *
1004  * @param The enviroment pinter.
1005  */
1006 static void fix_ls(env_t *env)
1007 {
1008   fixlist_entry_t *l;
1009   ir_node      *irn, *block, *pred, *val = NULL;
1010   ir_op        *op;
1011   int          i;
1012
1013   for (l = env->fix_ls; l; l = get_irn_link(irn)) {
1014     irn = l->irn;
1015
1016     op     = get_irn_op(irn);
1017     block  = get_nodes_block(irn);
1018     for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1019       pred = get_Block_cfgpred(block, i);
1020       pred = get_nodes_block(pred);
1021
1022       inc_irg_block_visited(current_ir_graph);
1023       val = find_vnum_value(pred, l->vnum);
1024
1025       if (val)
1026         break;
1027     }
1028
1029     if(val) {
1030       if(op == op_Store)
1031         set_Store_mem(irn, val);
1032       else
1033         if(op == op_Load)
1034           set_Load_mem(irn, val);
1035         else
1036           set_Call_mem(irn, val);
1037     }
1038   }
1039 }
1040
1041 /**
1042  * fix the Phi list
1043  *
1044  * @param The enviroment pinter.
1045  */
1046 static void fix_phis(env_t *env)
1047 {
1048   fixlist_entry_t *l;
1049   ir_node         *phi, *block, *pred, *val;
1050   int             i;
1051
1052   for (l = env->fix_phis; l; l = get_irn_link(phi)) {
1053     phi = l->irn;
1054
1055     block = get_nodes_block(phi);
1056     for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1057
1058       pred = get_Block_cfgpred(block, i);
1059       pred = get_nodes_block(pred);
1060
1061       inc_irg_block_visited(current_ir_graph);
1062       val = find_vnum_value(pred, l->vnum);
1063
1064       if (val)
1065         set_irn_n(phi, i, val);
1066     }
1067   }
1068 }
1069
1070
1071 /**
1072  * fix the Sync list
1073  *
1074  * @param The enviroment pinter.
1075  */
1076 static void fix_syncs(env_t *env)
1077 {
1078   syncs_fixlist_entry_t *l;
1079   ir_node               *sync, *block, *pred, *val;
1080   int                   i, k;
1081
1082
1083   for (l = env->fix_syncs; l; l = get_irn_link(sync)) {
1084     sync = l->irn;
1085     k = 0;
1086
1087     /* The sync block must have one predecessor, when it
1088        have unknown nodes as predecessor.*/
1089     block = get_nodes_block(sync);
1090     pred  = get_Block_cfgpred(block, 0);
1091     pred  = get_nodes_block(pred);
1092
1093     /* We first repair the global memory edge at the first position of sync predecessors.*/
1094     if (is_Unknown(get_irn_n(sync, 0))) {
1095       inc_irg_block_visited(current_ir_graph);
1096       val = find_vnum_value(pred, env->gl_mem_vnum);
1097
1098       if(val)
1099         set_irn_n(sync, 0, val);
1100     }
1101
1102     for (i = get_irn_arity(sync) - 1; i >= 1; --i) {
1103       /* We repair the leaves*/
1104
1105       assert(k <= ARR_LEN(l->accessed_vnum) && "The algorythm for sync repair is wron");
1106       if (is_Unknown(get_irn_n(sync, i))) {
1107         inc_irg_block_visited(current_ir_graph);
1108         val = find_vnum_value(pred, l->accessed_vnum[k++]);
1109
1110         if(val)
1111           set_irn_n(sync, i, val);
1112       }
1113     }
1114     DEL_ARR_F(l->accessed_vnum);
1115   }
1116 }
1117 /**
1118  * For the end node we must sync all memory edges.
1119  *
1120  * @param The enviroment pinter.
1121  */
1122 static void sync_mem_edges(env_t *env) {
1123
1124   value_arr_entry_t *val_arr;
1125   ir_node           **in, *sync, *Return, *Return_blk;
1126   int               i, vnum, vnum_state;
1127
1128   Return     = get_Block_cfgpred(get_irg_end_block(current_ir_graph), 0);
1129   Return_blk = get_nodes_block(Return);
1130   val_arr    = get_irn_link(Return_blk);
1131
1132   vnum_state = 0;
1133
1134   for(i = 0; i <= (int)env->gl_mem_vnum; i++)
1135     /* we get the current state of non saved scalars.*/
1136     if(val_arr[i].access_type <= 3)
1137       vnum_state++;
1138
1139   /* We allocate the memory, that we need for the predecessors of the sync.*/
1140   in = XMALLOCN(ir_node*, vnum_state);
1141
1142   /* The global memory edge is the first predecessor of this sync node.*/
1143   if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
1144     /* We must search through blocks for this memory state.*/
1145     inc_irg_block_visited(current_ir_graph);
1146     in[0] = find_vnum_value(Return_blk, env->gl_mem_vnum);
1147   }
1148   else
1149     in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
1150
1151
1152   for(i = 1, vnum = 0; vnum < (int)env->gl_mem_vnum; vnum++) {
1153
1154     if(val_arr[vnum].access_type <= 3) {
1155       /* we add the non saved scalars as predecessors of the sync.*/
1156
1157       if(val_arr[vnum].mem_edge_state == NULL) {
1158         /* We must search through blocks for this memory state.*/
1159         inc_irg_block_visited(current_ir_graph);
1160         in[i] = find_vnum_value(Return_blk, vnum);
1161       }
1162       else
1163         in[i] = val_arr[vnum].mem_edge_state;
1164       i++;
1165     }
1166   }
1167
1168   sync = new_r_Sync(current_ir_graph, Return_blk, vnum_state, in);
1169   set_Return_mem(Return, sync);
1170
1171   free(in);
1172 }
1173
1174 /**
1175  * Walker: allocate the value array for every block.
1176  *
1177  * @param block  A block from the current ir graph for that must be allocated a value array.
1178  * @param ctx    The enviroment pinter.
1179  */
1180 static void alloc_value_arr(ir_node *block, void *ctx)
1181 {
1182   env_t *env = ctx;
1183   int   i;
1184
1185   value_arr_entry_t *var_arr = obstack_alloc(&env->obst, sizeof(value_arr_entry_t) *(env->nvals + set_count(env->set_ent) + 1));
1186
1187   /* the value array is empty at start */
1188   memset(var_arr, 0, sizeof(value_arr_entry_t) * (env->nvals + set_count(env->set_ent) + 1));
1189   set_irn_link(block, var_arr);
1190
1191  /* We set the block value number state to optimal and later we update this.*/
1192   var_arr[env->vnum_state].access_type = env->nvals;
1193
1194   if(get_irg_start_block(current_ir_graph) == block)
1195     /* We initilize the startblocks array with the irg initilize memory, why
1196      * it must be the start point of all memory edges.*/
1197     for(i = (env->nvals + set_count(env->set_ent)) ; i >=0; i--)
1198       var_arr[i].mem_edge_state = get_irg_initial_mem(current_ir_graph);
1199
1200 }
1201
1202 /* Analyze call nodes to get information, if they store the address of a scalar.
1203  *
1204  * @param *irn   An ir node from the current_ir_graph.
1205  * @param *env   The enviroment pointer.
1206 */
1207 static void analyse_calls(ir_node *irn, void *ctx) {
1208
1209   int                 i, vnum;
1210   unsigned int        acces_type;
1211   ir_node             *param, *call_ptr, *blk;
1212   ir_entity           *meth_ent;
1213   sels_t              key_sels, *value_sels;
1214   call_access_t       key_call, *value_call;
1215   value_arr_entry_t   *val_arr;
1216   env_t               *env;
1217
1218   env = ctx;
1219   if (!is_Call(irn))
1220     return;
1221
1222   /* Calls that have a NoMem input do neither read nor write memory.
1223      We can completely ignore them here. */
1224   if (is_NoMem(get_Call_mem(irn)))
1225     return;
1226
1227   /* We iterate over the parameters of this call nodes.*/
1228   for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
1229     param = get_Call_param(irn, i);
1230     if (is_Sel(param)) {
1231       /* We have found a parameter with operation sel.*/
1232       key_sels.sel = param;
1233       value_sels   = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
1234       if(value_sels != NULL ) {
1235
1236         /* We have found a call, that have as parameter a sel from our set_sels.*/
1237         call_ptr = get_Call_ptr(irn);
1238
1239         if (is_SymConst(call_ptr) && get_SymConst_kind(call_ptr) == symconst_addr_ent) {
1240           meth_ent = get_SymConst_entity(call_ptr);
1241           /* we get the access type for our sel.*/
1242           acces_type = get_method_param_access(meth_ent, i);
1243         } else
1244           /* We can't analyze this function and we asume, that it store the address.*/
1245           acces_type = ptr_access_store;
1246
1247         /* we save the access type and this call in the array allocated for this block.
1248          * The value number of this entity get us the position in the array to save this
1249          * information. Why we expect more calls as one we allocate a set.*/
1250         vnum    = GET_ENT_VNUM(value_sels->ent);
1251         blk     = get_nodes_block(irn);
1252         val_arr = get_irn_link(blk);
1253
1254         if(val_arr[vnum].access_type > 3)
1255           /* The address of this entity have been stored.*/
1256           continue;
1257
1258         if(val_arr[vnum].calls == NULL)
1259           /* for this entity i have found the firs call in this block and we must allocate the set.*/
1260           val_arr[vnum].calls = new_set(call_cmp, 8);
1261
1262           /* This call performs anything with the scalar and we must mark it.*/
1263           key_call.call = irn;
1264           key_call.access_type = acces_type;
1265           value_call = set_insert(val_arr[vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
1266
1267         if(value_call->access_type < acces_type)
1268           /* this case tread, when a call access an entity more at once.
1269            * Than we must save the highest access type.*/
1270           value_call->access_type = acces_type;
1271
1272         if(acces_type > 3)
1273           /* This call save the address of our scalar and we can't
1274            * use the scalars of this entity for optimization as from now.
1275            * we mark this.*/
1276           val_arr[vnum].access_type = acces_type;
1277       }
1278     }
1279   }
1280 }
1281
1282 static int set_block_dominated_first_access(ir_node *blk, int vnum, unsigned int access) {
1283
1284   ir_node *idom, *succ;
1285   value_arr_entry_t *val_arr;
1286   int i, changes = 0;
1287
1288   idom = get_Block_idom(blk);
1289   for(i = get_Block_n_cfg_outs(idom) - 1; i >=1; i--) {
1290     succ = get_Block_cfg_out(idom, i);
1291     val_arr  = get_irn_link(succ);
1292     if(val_arr[vnum].access_type < 3) {
1293       val_arr[vnum].access_type = access;
1294       changes++;
1295     }
1296   }
1297   return changes;
1298 }
1299 /* Update the access information of a block if a predecessor of
1300  * this black have a higher access for an entity.
1301  *
1302  * @param *irn   An ir node from the current_ir_graph.
1303  * @param *env   The enviroment pointer.
1304  */
1305 static void set_block_access(ir_node *irn, void *ctx){
1306
1307   value_arr_entry_t *val_arr, *val_arr_pred;
1308   ent_leaves_t      *value_leaves;
1309   ir_node           *pred, *pred_blk, *leave;
1310   env_t             *env;
1311   int               i, vnum;
1312
1313   env     = ctx;
1314   val_arr = get_irn_link(irn);
1315
1316   for( i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
1317     /* We analyze the predecessors of this block to see if this block must
1318      * be updated.*/
1319     pred = get_Block_cfgpred(irn, i);
1320     pred_blk = get_nodes_block(pred);
1321
1322     val_arr_pred = get_irn_link(pred_blk);
1323
1324     for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1325       vnum = GET_ENT_VNUM(value_leaves->ent);
1326
1327       if((get_Block_n_cfgpreds(irn) > 1) && (val_arr[vnum].access_type > 3))
1328         env->changes =  set_block_dominated_first_access(irn, vnum, val_arr[vnum].access_type);
1329
1330       if((val_arr_pred[vnum].access_type > 3) && (val_arr[vnum].access_type < 3)) {
1331         /* We have found a block for update it access and value number information.*/
1332         val_arr[vnum].access_type = val_arr_pred[vnum].access_type;
1333         /* We update the access information of all leave, that belong to
1334          * this entity.*/
1335
1336         for(leave = pset_first(value_leaves->leaves); leave; leave = pset_next(value_leaves->leaves))
1337           val_arr[GET_IRN_VNUM(leave)].access_type = val_arr[vnum].access_type;
1338
1339         /* In this way can't be got the actuall number of value numbers.
1340         val_arr[env->vnum_state].access_type = val_arr_pred[env->vnum_state].access_type; */
1341         env->changes++;
1342       }
1343     }
1344   }
1345 }
1346 /* Free the allocated call sets.
1347  *
1348  * @param irn  A block form the ir graph.
1349  * @param env  The enviroment pinter.
1350  */
1351 static void free_call_info(ir_node *irn, void *ctx) {
1352
1353   int i;
1354   env_t             *env;
1355   value_arr_entry_t *val_arr;
1356
1357   env     = ctx;
1358   val_arr = get_irn_link(irn);
1359
1360   for(i = env->nvals + set_count(env->set_ent); i >= 0; i--) {
1361     if(val_arr[i].calls != NULL)
1362
1363       del_set(val_arr[i].calls);
1364   }
1365 }
1366
1367 static void print_block_state(ir_node *irn, void *ctx) {
1368
1369   value_arr_entry_t  *val_arr;
1370   ent_leaves_t       *value_leaves;
1371   call_access_t      *value_calls;
1372   env_t              *env;
1373   int                vnum;
1374
1375   env     = ctx;
1376   val_arr = get_irn_link(irn);
1377   ir_printf("\n\nThe actual value number state of this block is: %i \n",
1378             val_arr[env->vnum_state].access_type - 1);
1379
1380   for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1381
1382     vnum = GET_ENT_VNUM(value_leaves->ent);
1383     ir_printf("The entity %F access type in the block with nr %u is %i \n",
1384               value_leaves->ent, get_irn_node_nr(irn), val_arr[vnum].access_type);
1385
1386     if(val_arr[vnum].calls != NULL)
1387       for(value_calls = set_first(val_arr[vnum].calls); value_calls; value_calls = set_next(val_arr[vnum].calls))
1388
1389         ir_printf("A call with nr %i acess a element of this entity with access %u \n",
1390                   get_irn_node_nr(value_calls->call), value_calls->access_type);
1391   }
1392
1393 }
1394
1395 /** Optimize the found scalar replacements.
1396 *
1397 * @param set_sels  A set with all entities, that
1398 *                  have scala(s).
1399 * @param set_ent   A set with all sels nodes,
1400 *                  that belong to our scalars.
1401 * @param vnum      The number of scalars.
1402 */
1403 static void do_data_flow_scalar_replacement(set *set_ent, set *set_sels, int vnum) {
1404
1405   env_t env;
1406
1407   obstack_init(&env.obst);
1408   env.set_ent     = set_ent;
1409   env.set_sels    = set_sels;
1410   env.fix_ls      = NULL;
1411   env.fix_phis    = NULL;
1412   env.fix_syncs   = NULL;
1413   env.gl_mem_vnum = vnum - 2;
1414   env.vnum_state  = vnum - 1;
1415   /* nvals are vnum - 1, why we indicate with nvals the number
1416    * of memory edges we will produce. For vnum_state we don't
1417    * need to produce a memory edge.*/
1418   env.nvals       = vnum - 1;
1419   env.changes     = 1;
1420
1421   /* first step: allocate the value arrays for every block */
1422   irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
1423
1424   /* second step: we analyze all calls, that have as parameter scalar(s).
1425    * We mark the calls, that save the address of a scalar and we
1426    * mark the entity owner of this scalar as not optimizeble by now.*/
1427   irg_walk_graph(current_ir_graph, NULL, analyse_calls, &env);
1428
1429   while(env.changes) {
1430
1431
1432     env.changes  = 0;
1433     /*
1434     * third step: walk over the blocks of a graph and update
1435     * the information for the access of our scalars.
1436     */
1437     irg_block_walk_graph(current_ir_graph, NULL, set_block_access, &env);
1438
1439   }
1440
1441   // if(get_firm_verbosity())
1442     /* Debug info to see if analyse_calls work properly.*/
1443     irg_block_walk_graph(current_ir_graph, NULL, print_block_state, &env);
1444
1445   /*
1446    * fourth step: walk over the graph blockwise in topological order
1447    * and split the memrory edge.
1448    */
1449   inc_irg_block_visited(current_ir_graph);
1450   irg_walk_blkwise_graph(current_ir_graph, NULL, split_memory_edge, &env);
1451
1452
1453
1454   /* fifth step: fix all nodes, that have as predecessor Unknown.*/
1455   fix_ls(&env);
1456   fix_phis(&env);
1457   fix_syncs(&env);
1458
1459   /* sixth step: sync memory enges for the end block.*/
1460   sync_mem_edges(&env);
1461
1462   /*seventh step: free the allocated memory*/
1463   irg_block_walk_graph(current_ir_graph, NULL, free_call_info, &env);
1464   obstack_free(&env.obst, NULL);
1465 }
1466
1467 /*
1468  * Find possible scalar replacements
1469  *
1470  * @param irg  The current ir graph.
1471  */
1472 void data_flow_scalar_replacement_opt(ir_graph *irg) {
1473
1474   int          i, vnum = 0;
1475   ir_node      *irg_frame;
1476   set          *set_sels;
1477   set          *set_ent;
1478   ent_leaves_t key_leaves, *value_leaves;
1479
1480
1481   if (! get_opt_scalar_replacement())
1482     return;
1483
1484   set_sels = new_set(sels_cmp, 8);
1485   set_ent  = new_set(ent_leaves_t_cmp, 8);
1486
1487   /* Call algorithm that remove the critical edges of a ir graph. */
1488   remove_critical_cf_edges(irg);
1489
1490   /* Call algorithm that computes the out edges.*/
1491   assure_irg_outs(irg);
1492
1493   /* Call algorithm that computes the loop information.*/
1494   construct_cf_backedges(irg);
1495
1496   /* Call algorithm that computes the dominance information.*/
1497   assure_doms(irg);
1498
1499   /* Find possible scalar replacements */
1500   if (find_possible_replacements(irg)) {
1501
1502     /* Insert in set the scalar replacements. */
1503     irg_frame = get_irg_frame(irg);
1504
1505     for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
1506       ir_node *succ = get_irn_out(irg_frame, i);
1507
1508       if (is_Sel(succ)) {
1509         ir_entity *ent = get_Sel_entity(succ);
1510
1511         if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
1512           continue;
1513         /* we have found a entity, that have scalars and we insert it to our set_ent*/
1514         key_leaves.ent = ent;
1515         key_leaves.leaves = new_pset(leave_cmp, 8);
1516         value_leaves = set_insert(set_ent, &key_leaves, sizeof(key_leaves), HASH_PTR(ent));
1517
1518         /* We allocate for every leave sel a vnum.*/
1519         vnum = allocate_value_numbers(set_sels, value_leaves->leaves, ent, vnum);
1520       }
1521     }
1522
1523     /* Allocate value number for the globule memory edge.
1524      * and a value number for the value numbers state.*/
1525     vnum = vnum + 2;
1526
1527     /* Allocate value numbers for the entities .*/
1528     for(i = vnum,value_leaves = set_first(set_ent); value_leaves; i++, value_leaves = set_next(set_ent))
1529       SET_ENT_VNUM(value_leaves->ent, i);
1530
1531     if (vnum)
1532       do_data_flow_scalar_replacement(set_ent, set_sels, vnum);
1533
1534     /*free the allocated memory.*/
1535     for(value_leaves = set_first(set_ent); value_leaves; value_leaves = set_next(set_ent))
1536       del_pset(value_leaves->leaves);
1537     del_set(set_ent);
1538     del_set(set_sels);
1539   }
1540 }