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
9 * Copyright: (c) 1998-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 #include "data_flow_scalar_replace.h"
39 #include "analyze_irg_args.h"
41 #include "compute_loop_info.h"
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))
51 typedef struct _ent_leaves_t{
52 entity *ent; /**< An entity, that contains scalars for replace.*/
53 pset *leaves; /**< All leaves of this entity.*/
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.*/
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.*/
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.*/
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;
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.*/
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
96 * An access path, used to assign value numbers
97 * to variables that will be scalar replaced
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 */
106 * environment for memory walker
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.*/
124 * Compare two elements of the ent_leaves_t set.
126 * @return 0 if they are identically
128 static int ent_leaves_t_cmp(const void *elt, const void *key, size_t size)
130 const ent_leaves_t *c1 = elt;
131 const ent_leaves_t *c2 = key;
133 return c1->ent != c2->ent;
137 * Compare two elements of the ent_access_t set.
139 * @return 0 if they are identically
141 static int ent_cmp(const void *elt, const void *key)
143 const entity *c1 = elt;
144 const entity *c2 = key;
150 * Compare two elements of the sels_t set.
152 * @return 0 if they are identically
154 static int sels_cmp(const void *elt, const void *key, size_t size)
156 const sels_t *c1 = elt;
157 const sels_t *c2 = key;
159 return c1->sel != c2->sel;
163 * Compare two elements of the leave_t set.
165 * @return 0 if they are identically
167 static int leave_cmp(const void *elt, const void *key)
169 const ir_node *c1 = elt;
170 const ir_node *c2 = key;
172 return c1->attr.s.ent != c2->attr.s.ent;
176 * Compare two elements of the call_access_t set.
178 * @return 0 if they are identically
180 static int call_cmp(const void *elt, const void *key, size_t size)
182 const call_access_t *c1 = elt;
183 const call_access_t *c2 = key;
185 return c1->call != c2->call;
191 * @return 0 if they are identically
193 static int path_cmp(const void *elt, const void *key, size_t size)
195 const path_t *p1 = elt;
196 const path_t *p2 = key;
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]));
203 * Calculate a hash value for a path.
205 static unsigned path_hash(const path_t *path)
210 for (i = 0; i < path->path_len; ++i)
211 hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
217 * Returns non-zero, if all induces of a Sel node are constants.
219 * @param sel the Sel node that will be checked
221 static int is_const_sel(ir_node *sel) {
222 int i, n = get_Sel_n_indexs(sel);
224 for (i = 0; i < n; ++i) {
225 ir_node *idx = get_Sel_index(sel, i);
227 if (get_irn_op(idx) != op_Const)
234 * Returns non-zero, if the address of an entity
235 * represented by a Sel node (or it's successor Sels) is taken.
237 static int is_address_taken(ir_node *sel)
241 if (! is_const_sel(sel))
244 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
245 ir_node *succ = get_irn_out(sel, i);
247 switch (get_irn_opcode(succ)) {
249 /* ok, we just load from that entity */
253 /* check that Sel is not the Store's value */
254 if (get_Store_value(succ) == sel)
259 /* Check the Sel successor of Sel */
260 int res = is_address_taken(succ);
268 /* The address of an entity is given as a parameter.
269 * We analyzes that later and optimizes this scalar
275 /* another op, the address is taken */
283 * Link all Sels with the entity.
285 * @param ent the entity that will be scalar replaced
286 * @param sel a Sel node that selects some fields of this entity
288 static void link_all_leave_sels(entity *ent, ir_node *sel)
292 n = get_irn_n_outs(sel);
293 for (i = 0; i < n; ++i) {
294 ir_node *succ = get_irn_out(sel, i);
296 if (get_irn_op(succ) == op_Sel)
297 link_all_leave_sels(ent, succ);
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
305 if (irn_visited(sel))
309 * we link the sels to the entity.
311 set_irn_link(sel, get_entity_link(ent));
312 set_entity_link(ent, sel);
314 mark_irn_visited(sel);
317 /* we need a special address that serves as an address taken marker */
319 static void *ADDRESS_TAKEN = &_x;
322 * Find possible scalar replacements.
324 * @param irg an IR graph
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.
332 * @return non-zero if at least one entity could be replaced
335 static int find_possible_replacements(ir_graph *irg)
337 ir_node *irg_frame = get_irg_frame(irg);
341 inc_irg_visited(irg);
343 n = get_irn_n_outs(irg_frame);
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.
352 for (i = 0; i < n; ++i) {
353 ir_node *succ = get_irn_out(irg_frame, i);
355 if (get_irn_op(succ) == op_Sel) {
356 entity *ent = get_Sel_entity(succ);
357 set_entity_link(ent, NULL);
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.
366 for (i = 0; i < n; ++i) {
367 ir_node *succ = get_irn_out(irg_frame, i);
369 if (get_irn_op(succ) == op_Sel) {
370 entity *ent = get_Sel_entity(succ);
373 if (get_entity_link(ent) == ADDRESS_TAKEN)
376 ent_type = get_entity_type(ent);
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 */
383 set_entity_link(ent, ADDRESS_TAKEN);
386 /* possible found one */
387 if (get_entity_link(ent) == NULL)
389 link_all_leave_sels(ent, succ);
398 static int is_leave_sel(ir_node *sel) {
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)
412 * Return a path from the Sel node sel to it's root.
414 * @param sel the Sel node
415 * @param len the length of the path so far
417 static path_t *find_path(ir_node *sel, unsigned len)
421 ir_node *pred = get_Sel_ptr(sel);
423 /* the current Sel node will add some path elements */
424 n = get_Sel_n_indexs(sel);
427 if (get_irn_op(pred) != op_Sel) {
428 /* we found the root */
430 res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
434 res = find_path(pred, len);
436 pos = res->path_len - len;
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);
442 if(get_irn_op(index) == op_Const)
443 res->path[pos++].tv = get_Const_tarval(index);
449 * Allocate value numbers for the leaves
450 * in our found entities.
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
458 * @return the next free value number
460 static unsigned allocate_value_numbers(set *set_sels, pset *leaves, entity *ent, unsigned vnum)
465 set *pathes = new_set(path_cmp, 8);
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);
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. */
476 set_insert(set_sels, &key_sels, sizeof(key_sels), HASH_PTR(sel));
478 if(! is_leave_sel(sel))
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)));
483 key = find_path(sel, 0);
484 path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
487 SET_IRN_VNUM(sel, path->vnum);
492 set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
494 SET_IRN_VNUM(sel, key->vnum);
500 set_entity_link(ent, NULL);
504 * Add a sync node to it fix list.
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.
510 static void add_sync_to_fixlist(ir_node *sync, int *unk_vnum, env_t *env) {
512 syncs_fixlist_entry_t *s;
514 s = obstack_alloc(&env->obst, sizeof(*s));
516 s->accessed_vnum = unk_vnum;
517 set_irn_link(sync, env->fix_syncs);
521 * Add a ir node to it fix list.
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.
527 static void add_ls_to_fixlist(ir_node *irn, int vnum, env_t *env) {
531 l = obstack_alloc(&env->obst, sizeof(*l));
535 if(get_irn_op(irn) == op_Phi) {
536 set_irn_link(l->irn, env->fix_phis);
539 set_irn_link(l->irn, env->fix_ls);
544 static void add_mem_edge(value_arr_entry_t *val_arr, int vnum, ir_node ***in, int **accessed_vnum) {
546 if(val_arr[vnum].mem_edge_state != NULL)
547 ARR_APP1(ir_node *, *in, val_arr[vnum].mem_edge_state);
549 ARR_APP1(int, *accessed_vnum, vnum);
550 ARR_APP1(ir_node *, *in, new_Unknown(mode_M));
554 * The function handles the scalars, that wase stored
557 * @param blk The block, that must be handled.
558 * @param env The enviroment pinter.
561 /* Return the memory successor of the call node.*/
562 static ir_node *get_Call_mem_out(ir_node *call) {
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)
572 /* is not reachable*/
577 static void sync_stored_scalars(ir_node *blk, env_t *env) {
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.*/
588 val_arr_blk = get_irn_link(blk);
590 for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent)) {
593 if(val_arr_blk[GET_ENT_VNUM(value_ent->ent)].access_type <= 3)
594 /* This entity is not stored in this block.*/
597 for(i = get_Block_n_cfgpreds(blk) - 1; i >= 0; i--) {
599 pred = get_Block_cfgpred(blk, i);
600 pred = get_nodes_block(pred);
601 val_arr = get_irn_link(pred);
603 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type == SYNCED)
604 /* This entity was synced.*/
607 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type <= 3) {
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));
618 val_arr = get_irn_link(sync_blk);
619 /* An array to save the memory edges, that must be
621 in = NEW_ARR_F(ir_node *, 1);
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);
630 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
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);
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);
652 * The function split the memory edge of load and store nodes, that have
653 * as predecessor a scalar
655 * @param irn The node, that memory edge must be spleted.
656 * @param env The enviroment pinter.
658 static void split_ls_mem_edge(ir_node *irn, env_t *env) {
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;
667 op = get_irn_op(irn);
670 key_sels.sel = get_Load_ptr(irn);
672 key_sels.sel = get_Store_ptr(irn);
674 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
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.*/
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.");
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.");
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);
695 if(val_arr[ent_vnum].access_type == 0)
696 /* We have found a scalar, that address is not stored as jet.*/
699 /* This scalar have been stored.*/
700 i = env->gl_mem_vnum;
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);
709 /* We have split the memory edge and the current state is saved.*/
710 mem_state = val_arr[i].mem_edge_state;
712 /* We set this Load or Store to the memory edge of this
715 set_Load_mem(irn, mem_state);
717 set_Store_mem(irn, mem_state);
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;
725 val_arr[i].mem_edge_state = get_irn_out(irn, 1);
730 * The function split the memory edge of phi nodes, that have
731 * as predecessor a scalar
733 * @param irn The phi node, that memory edge must be spleted.
734 * @param env The enviroment pinter.
736 static void split_phi_mem_edge(ir_node *irn, env_t *env) {
738 ir_node *irn_blk, *unk, *leave, **in;
740 ent_leaves_t *value_ent;
741 value_arr_entry_t *val_arr;
743 irn_blk = get_nodes_block(irn);
744 val_arr = get_irn_link(irn_blk);
746 n = get_Block_n_cfgpreds(irn_blk);
748 in = alloca(sizeof(*in) * n);
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)){
755 unk = new_Unknown(mode_M);
756 for (j = n - 1; j >= 0; --j)
759 val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_r_Phi(current_ir_graph, irn_blk, n, in, mode_M);
761 add_ls_to_fixlist(val_arr[GET_IRN_VNUM(leave)].mem_edge_state, GET_IRN_VNUM(leave), env);
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;
770 * The function handles the call nodes, that have
771 * as parameter a scalar
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) {
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;
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.*/
789 if(get_irn_node_nr(call) == 2763)
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
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);
801 /* We get the memory successor of the call node.
802 * It is the new memory state for all synced memory
804 new_mem_state = get_Call_mem_out(call);
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);
812 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
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);
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));
824 value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
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) {
830 case ptr_access_none :
831 /* In this case we have nothing to do.*/
834 case ptr_access_read:
835 case ptr_access_write:
837 /* All this cases must be traded equal.*/
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);
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;
854 /* We must update the global memory state.*/
855 val_arr[env->gl_mem_vnum].mem_edge_state = new_mem_state;
857 if(ARR_LEN(in) == 1) {
858 /* we must set the call memory to gobale momory*/
859 set_Call_mem(call,in[0]);
862 /* We add this call node to the call fix list..*/
863 add_ls_to_fixlist(call, env->gl_mem_vnum, env);
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) {
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);
883 * The function split the memory edge from the passed
884 * ir node if this is needed
886 * @param irn The node, that memory edge must be spleted.
887 * @param env The enviroment pinter.
889 static void split_memory_edge(ir_node *irn, void *ctx) {
892 ir_node *sel, *irn_blk;
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.*/
900 op = get_irn_op(irn);
905 irn_blk = get_nodes_block(irn);
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);
913 if(op == op_Load || op == op_Store)
915 split_ls_mem_edge(irn, env);
918 if (op == op_Phi && get_irn_mode(irn) == mode_M) {
920 * found a memory Phi: Here, we must create new Phi nodes
922 split_phi_mem_edge(irn, env);
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)
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));
937 for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
939 sel = get_Call_param(irn, i);
941 if(get_irn_op(sel) == op_Sel) {
943 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
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));
952 if(pset_count(accessed_entities))
953 split_call_mem_edge(env, irn, accessed_entities);
955 del_pset(accessed_entities);
962 * searches through blocks beginning from block for value
963 * vnum and return it.
965 * @param block A block from the current ir graph.
966 * @param vnum The value number, that must be found.
968 static ir_node *find_value(ir_node *block, unsigned vnum)
970 value_arr_entry_t *val_arr;
974 if (Block_not_block_visited(block)) {
975 mark_Block_block_visited(block);
977 val_arr = get_irn_link(block);
979 if (val_arr[vnum].mem_edge_state)
980 return val_arr[vnum].mem_edge_state;
982 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
983 ir_node *pred = get_Block_cfgpred(block, i);
985 res = find_value(get_nodes_block(pred), vnum);
994 * fix the Load/Store or Call list
996 * @param The enviroment pinter.
998 static void fix_ls(env_t *env)
1001 ir_node *irn, *block, *pred, *val;
1005 for (l = env->fix_ls; l; l = get_irn_link(irn)) {
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);
1014 inc_irg_block_visited(current_ir_graph);
1015 val = find_value(pred, l->vnum);
1023 set_Store_mem(irn, val);
1026 set_Load_mem(irn, val);
1028 set_Call_mem(irn, val);
1036 * @param The enviroment pinter.
1038 static void fix_phis(env_t *env)
1041 ir_node *phi, *block, *pred, *val;
1044 for (l = env->fix_phis; l; l = get_irn_link(phi)) {
1047 block = get_nodes_block(phi);
1048 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1050 pred = get_Block_cfgpred(block, i);
1051 pred = get_nodes_block(pred);
1053 inc_irg_block_visited(current_ir_graph);
1054 val = find_value(pred, l->vnum);
1057 set_irn_n(phi, i, val);
1066 * @param The enviroment pinter.
1068 static void fix_syncs(env_t *env)
1070 syncs_fixlist_entry_t *l;
1071 ir_node *sync, *block, *pred, *val;
1075 for (l = env->fix_syncs; l; l = get_irn_link(sync)) {
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);
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);
1091 set_irn_n(sync, 0, val);
1094 for (i = get_irn_arity(sync) - 1; i >= 1; --i) {
1095 /* We repair the leaves*/
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++]);
1103 set_irn_n(sync, i, val);
1106 DEL_ARR_F(l->accessed_vnum);
1110 * For the end node we must sync all memory edges.
1112 * @param The enviroment pinter.
1114 static void sync_mem_edges(env_t *env) {
1116 value_arr_entry_t *val_arr;
1117 ir_node **in, *sync, *Return, *Return_blk;
1118 int i, vnum, vnum_state;
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);
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)
1131 /* We allocate the memory, that we need for the predecessors of the sync.*/
1132 in = malloc(sizeof(ir_node*) *vnum_state);
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);
1141 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
1144 for(i = 1, vnum = 0; vnum < (int)env->gl_mem_vnum; vnum++) {
1146 if(val_arr[vnum].access_type <= 3) {
1147 /* we add the non saved scalars as predecessors of the sync.*/
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);
1155 in[i] = val_arr[vnum].mem_edge_state;
1160 sync = new_r_Sync(current_ir_graph, Return_blk, vnum_state, in);
1161 set_Return_mem(Return, sync);
1167 * Walker: allocate the value array for every block.
1169 * @param block A block from the current ir graph for that must be allocated a value array.
1170 * @param ctx The enviroment pinter.
1172 static void alloc_value_arr(ir_node *block, void *ctx)
1177 value_arr_entry_t *var_arr = obstack_alloc(&env->obst, sizeof(value_arr_entry_t) *(env->nvals + set_count(env->set_ent) + 1));
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);
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;
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);
1194 /* Analyze call nodes to get information, if they store the address of a scalar.
1196 * @param *irn An ir node from the current_ir_graph.
1197 * @param *env The enviroment pointer.
1199 static void analyse_calls(ir_node *irn, void *ctx) {
1202 unsigned int acces_type;
1203 ir_node *param, *call_ptr, *blk;
1206 sels_t key_sels, *value_sels;
1207 call_access_t key_call, *value_call;
1208 value_arr_entry_t *val_arr;
1212 if(get_irn_op(irn) != op_Call)
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)
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 ) {
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);
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);
1238 /* We can't analyze this function and we asume, that it store the address.*/
1239 acces_type = ptr_access_store;
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);
1248 if(val_arr[vnum].access_type > 3)
1249 /* The address of this entity have been stored.*/
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);
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));
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;
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.
1270 val_arr[vnum].access_type = acces_type;
1276 static int have_blk_phi_mem(ir_node *blk) {
1281 for(i = get_irn_n_outs(blk) - 1; i >= 0; i--) {
1283 out = get_irn_out(blk, i);
1284 if(get_irn_op(out) == op_Phi)
1291 static int set_block_dominated_first_access(ir_node *blk, int vnum, unsigned int access) {
1293 ir_node *idom, *succ;
1294 value_arr_entry_t *val_arr;
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;
1308 /* Update the access information of a block if a predecessor of
1309 * this black have a higher access for an entity.
1311 * @param *irn An ir node from the current_ir_graph.
1312 * @param *env The enviroment pointer.
1314 static void set_block_access(ir_node *irn, void *ctx){
1316 value_arr_entry_t *val_arr, *val_arr_pred;
1317 ent_leaves_t *value_leaves;
1318 ir_node *pred, *pred_blk, *leave;
1323 val_arr = get_irn_link(irn);
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
1328 pred = get_Block_cfgpred(irn, i);
1329 pred_blk = get_nodes_block(pred);
1331 val_arr_pred = get_irn_link(pred_blk);
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);
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);
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
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;
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; */
1355 /* Free the allocated call sets.
1357 * @param irn A block form the ir graph.
1358 * @param env The enviroment pinter.
1360 static void free_call_info(ir_node *irn, void *ctx) {
1364 value_arr_entry_t *val_arr;
1367 val_arr = get_irn_link(irn);
1369 for(i = env->nvals + set_count(env->set_ent); i >= 0; i--) {
1370 if(val_arr[i].calls != NULL)
1372 del_set(val_arr[i].calls);
1376 static void print_block_state(ir_node *irn, void *ctx) {
1378 value_arr_entry_t *val_arr;
1379 ent_leaves_t *value_leaves;
1380 call_access_t *value_calls;
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);
1389 for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
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);
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))
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);
1404 /** Optimize the found scalar replacements.
1406 * @param set_sels A set with all entities, that
1408 * @param set_ent A set with all sels nodes,
1409 * that belong to our scalars.
1410 * @param vnum The number of scalars.
1412 static void do_data_flow_scalar_replacement(set *set_ent, set *set_sels, int vnum) {
1416 obstack_init(&env.obst);
1417 env.set_ent = set_ent;
1418 env.set_sels = set_sels;
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;
1430 /* first step: allocate the value arrays for every block */
1431 irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
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);
1438 while(env.changes) {
1443 * third step: walk over the blocks of a graph and update
1444 * the information for the access of our scalars.
1446 irg_block_walk_graph(current_ir_graph, NULL, set_block_access, &env);
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);
1455 * fourth step: walk over the graph blockwise in topological order
1456 * and split the memrory edge.
1458 inc_irg_block_visited(current_ir_graph);
1459 irg_walk_blkwise_graph(current_ir_graph, NULL, split_memory_edge, &env);
1463 /* fifth step: fix all nodes, that have as predecessor Unknown.*/
1468 /* sixth step: sync memory enges for the end block.*/
1469 sync_mem_edges(&env);
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);
1477 * Find possible scalar replacements
1479 * @param irg The current ir graph.
1481 void data_flow_scalar_replacement_opt(ir_graph *irg) {
1487 ent_leaves_t key_leaves, *value_leaves;
1490 if (! get_opt_scalar_replacement())
1493 set_sels = new_set(sels_cmp, 8);
1494 set_ent = new_set(ent_leaves_t_cmp, 8);
1496 /* Call algorithm that remove the critical edges of a ir graph. */
1497 remove_critical_cf_edges(irg);
1499 /* Call algorithm that computes the out edges.*/
1500 if (get_irg_outs_state(irg) != outs_consistent)
1501 compute_irg_outs(irg);
1503 /* Call algorithm that computes the loop information.*/
1504 compute_loop_info(irg);
1505 /* Call algorithm that computes the dominance information.*/
1508 /* Find possible scalar replacements */
1509 if (find_possible_replacements(irg)) {
1511 /* Insert in set the scalar replacements. */
1512 irg_frame = get_irg_frame(irg);
1514 for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
1515 ir_node *succ = get_irn_out(irg_frame, i);
1517 if (get_irn_op(succ) == op_Sel) {
1518 entity *ent = get_Sel_entity(succ);
1520 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
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));
1527 /* We allocate for every leave sel a vnum.*/
1528 vnum = allocate_value_numbers(set_sels, value_leaves->leaves, ent, vnum);
1532 if(get_firm_verbosity())
1533 printf("vnumber in data flow= %i\n", vnum);
1535 /* Allocate value number for the globule memory edge.
1536 * and a value number for the value numbers state.*/
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);
1544 do_data_flow_scalar_replacement(set_ent, set_sels, vnum);
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);