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