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 ir_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 ir_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 ir_entity *c1 = elt;
144 const ir_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 ir_node *c1 = (ir_node *)elt;
170 ir_node *c2 = (ir_node *)key;
172 return get_Sel_entity(c1) != get_Sel_entity(c2);
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(ir_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 ir_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 ir_entity *ent = get_Sel_entity(succ);
373 if (get_entity_link(ent) == ADDRESS_TAKEN)
377 * Beware: in rare cases even entities on the frame might be
378 * volatile. This might happen if the entity serves as a store
379 * to a value that must survive a exception. Do not optimize
380 * such entities away.
382 if (get_entity_volatility(ent) == volatility_is_volatile) {
383 set_entity_link(ent, ADDRESS_TAKEN);
387 ent_type = get_entity_type(ent);
389 /* we can handle arrays, structs and atomic types yet */
390 if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
391 if (is_address_taken(succ)) {
392 if (get_entity_link(ent)) /* killing one */
394 set_entity_link(ent, ADDRESS_TAKEN);
397 /* possible found one */
398 if (get_entity_link(ent) == NULL)
400 link_all_leave_sels(ent, succ);
409 static int is_leave_sel(ir_node *sel) {
413 for(i = get_irn_n_outs(sel) - 1; i >= 0; i--) {
414 succ = get_irn_out(sel, i);
415 if(get_irn_op(succ) == op_Sel)
423 * Return a path from the Sel node sel to it's root.
425 * @param sel the Sel node
426 * @param len the length of the path so far
428 static path_t *find_path(ir_node *sel, unsigned len)
432 ir_node *pred = get_Sel_ptr(sel);
434 /* the current Sel node will add some path elements */
435 n = get_Sel_n_indexs(sel);
438 if (get_irn_op(pred) != op_Sel) {
439 /* we found the root */
441 res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
445 res = find_path(pred, len);
447 pos = res->path_len - len;
449 res->path[pos++].ent = get_Sel_entity(sel);
450 for (i = 0; i < n; ++i) {
451 ir_node *index = get_Sel_index(sel, i);
453 if(get_irn_op(index) == op_Const)
454 res->path[pos++].tv = get_Const_tarval(index);
460 * Allocate value numbers for the leaves
461 * in our found entities.
463 * @param sels a set that will contain all Sels that have a value number
464 * @param ent the entity that will be scalar replaced
465 * @param vnum the first value number we can assign
466 * @param modes a flexible array, containing all the modes of
469 * @return the next free value number
471 static unsigned allocate_value_numbers(set *set_sels, pset *leaves, ir_entity *ent, unsigned vnum)
476 set *pathes = new_set(path_cmp, 8);
478 /* visit all Sel nodes in the chain of the entity */
479 for (sel = get_entity_link(ent); sel; sel = next) {
480 next = get_irn_link(sel);
482 /* we save for every sel it root entity, why
483 * we need this information, when we split the memory edge,
484 * and we must mark this sel for later. */
487 set_insert(set_sels, &key_sels, sizeof(key_sels), HASH_PTR(sel));
489 if(! is_leave_sel(sel))
491 /* We have found a leave and we add it to the pset of this entity.*/
492 pset_insert(leaves, sel, HASH_PTR(get_Sel_entity(sel)));
494 key = find_path(sel, 0);
495 path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
498 SET_IRN_VNUM(sel, path->vnum);
503 set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
505 SET_IRN_VNUM(sel, key->vnum);
511 set_entity_link(ent, NULL);
515 * Add a sync node to it fix list.
517 * @param sync The sync node, that myst be addet to the fix list.
518 * @param unk_vnum An array whit the value number, that are synced with this sync node.
519 * @param env The enviroment pinter.
521 static void add_sync_to_fixlist(ir_node *sync, int *unk_vnum, env_t *env) {
523 syncs_fixlist_entry_t *s;
525 s = obstack_alloc(&env->obst, sizeof(*s));
527 s->accessed_vnum = unk_vnum;
528 set_irn_link(sync, env->fix_syncs);
532 * Add a ir node to it fix list.
534 * @param irn The ir node, that myst be addet to the fix list.
535 * @param vnum The value number, that must baceme this ir node as predecessor later.
536 * @param env The enviroment pinter.
538 static void add_ls_to_fixlist(ir_node *irn, int vnum, env_t *env) {
542 l = obstack_alloc(&env->obst, sizeof(*l));
546 if(get_irn_op(irn) == op_Phi) {
547 set_irn_link(l->irn, env->fix_phis);
550 set_irn_link(l->irn, env->fix_ls);
555 static void add_mem_edge(value_arr_entry_t *val_arr, int vnum, ir_node ***in, int **accessed_vnum) {
557 if(val_arr[vnum].mem_edge_state != NULL)
558 ARR_APP1(ir_node *, *in, val_arr[vnum].mem_edge_state);
560 ARR_APP1(int, *accessed_vnum, vnum);
561 ARR_APP1(ir_node *, *in, new_Unknown(mode_M));
565 * The function handles the scalars, that wase stored
568 * @param blk The block, that must be handled.
569 * @param env The enviroment pinter.
572 /* Return the memory successor of the call node.*/
573 static ir_node *get_Call_mem_out(ir_node *call) {
578 for(i = get_irn_n_outs(call) - 1; i >= 0; i--) {
579 mem = get_irn_out(call, i);
580 if(get_irn_mode(mem) == mode_M)
583 /* is not reachable*/
588 static void sync_stored_scalars(ir_node *blk, env_t *env) {
591 int *unk_vnum; /**< An arraw, where are saved the value number, that
592 are synced from this sync node.*/
593 ent_leaves_t *value_ent;
594 value_arr_entry_t *val_arr_blk, *val_arr;
595 ir_node *pred, *leave, *sync, **in;
596 ir_node *sync_blk; /**< The block, where the sync node must be created.*/
599 val_arr_blk = get_irn_link(blk);
601 for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent)) {
604 if(val_arr_blk[GET_ENT_VNUM(value_ent->ent)].access_type <= 3)
605 /* This entity is not stored in this block.*/
608 for(i = get_Block_n_cfgpreds(blk) - 1; i >= 0; i--) {
610 pred = get_Block_cfgpred(blk, i);
611 pred = get_nodes_block(pred);
612 val_arr = get_irn_link(pred);
614 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type == SYNCED)
615 /* This entity was synced.*/
618 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type <= 3) {
620 /* To avoid repeated sync of this entity in this block.*/
621 val_arr[GET_ENT_VNUM(value_ent->ent)].access_type = SYNCED;
622 /* In this predecessor block is this entity not acessed.
623 * We must sync in the end ot this block.*/
624 if(get_Block_n_cfgpreds(blk) > 1)
625 sync_blk = get_nodes_block(get_Block_cfgpred(blk, i));
629 val_arr = get_irn_link(sync_blk);
630 /* An array to save the memory edges, that must be
632 in = NEW_ARR_F(ir_node *, 1);
634 /* An array to save the value numbers,
635 * that must be repaired.*/
636 unk_vnum = NEW_ARR_F(int, 0);
637 /* The global memory edge.*/
638 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL)
639 in[0] = new_Unknown(mode_M);
641 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
643 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves))
644 /* All this memory edges must be synced.*/
645 add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &unk_vnum);
647 /* We create the sync and set it in the global memory state.*/
648 sync = new_r_Sync(current_ir_graph, sync_blk, ARR_LEN(in), in);
649 /* We must check this, why it is possible to get a Bad node
650 * form new_r_Sync(), when the node can be optimized.
651 * In this case we must do nothing.*/
652 if(get_irn_op(sync) == op_Sync) {
653 val_arr[env->gl_mem_vnum].mem_edge_state = sync;
654 /* We add this sync node to the sync's fix list.*/
655 add_sync_to_fixlist(val_arr[env->gl_mem_vnum].mem_edge_state, unk_vnum, env);
663 * The function split the memory edge of load and store nodes, that have
664 * as predecessor a scalar
666 * @param irn The node, that memory edge must be spleted.
667 * @param env The enviroment pinter.
669 static void split_ls_mem_edge(ir_node *irn, env_t *env) {
672 ir_node *leave, *irn_blk, *mem_state, *new_mem_state;
673 unsigned ent_vnum, sel_vnum, i;
674 value_arr_entry_t *val_arr;
675 sels_t key_sels, *value_sels;
676 ent_leaves_t key_ent, *value_ent;
678 op = get_irn_op(irn);
681 key_sels.sel = get_Load_ptr(irn);
683 key_sels.sel = get_Store_ptr(irn);
685 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
687 if(value_sels != NULL) {
688 /* we have found a load or store, that use a sel of our set
689 * and we must split or extend, if the memory edge have been
690 * split for this sel, the memory edge.*/
692 key_ent.ent = value_sels->ent;
693 value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
694 /*To check if the enities set is right filled. */
695 assert(value_ent && " This sel's entity isn't int the entity set.");
697 leave = pset_find(value_ent->leaves, key_sels.sel, HASH_PTR(get_Sel_entity(key_sels.sel)));
698 /*To check if the leaves set is right filled. */
699 assert(leave && "Anything in data_flow_scalar_replacment algorithm is wrong.");
701 ent_vnum = GET_ENT_VNUM(value_ent->ent);
702 sel_vnum = GET_IRN_VNUM(leave);
703 irn_blk = get_nodes_block(irn);
704 val_arr = get_irn_link(irn_blk);
706 if(val_arr[ent_vnum].access_type == 0)
707 /* We have found a scalar, that address is not stored as jet.*/
710 /* This scalar have been stored.*/
711 i = env->gl_mem_vnum;
713 if(val_arr[i].mem_edge_state == NULL) {
714 /* We split now for this sel the memory edge in this block.*/
715 mem_state = new_Unknown(mode_M);
716 /* We must mark this node to fix later*/
717 add_ls_to_fixlist(irn, i, env);
720 /* We have split the memory edge and the current state is saved.*/
721 mem_state = val_arr[i].mem_edge_state;
723 /* We set this Load or Store to the memory edge of this
726 set_Load_mem(irn, mem_state);
728 set_Store_mem(irn, mem_state);
730 /* When we have split or extended the memory edge we must
731 * update the memory_edge_state of this sel*/
732 new_mem_state = get_irn_out(irn, 0);
733 if(get_irn_mode(new_mem_state) == mode_M)
734 val_arr[i].mem_edge_state = new_mem_state;
736 val_arr[i].mem_edge_state = get_irn_out(irn, 1);
741 * The function split the memory edge of phi nodes, that have
742 * as predecessor a scalar
744 * @param irn The phi node, that memory edge must be spleted.
745 * @param env The enviroment pinter.
747 static void split_phi_mem_edge(ir_node *irn, env_t *env) {
749 ir_node *irn_blk, *unk, *leave, **in;
751 ent_leaves_t *value_ent;
752 value_arr_entry_t *val_arr;
754 irn_blk = get_nodes_block(irn);
755 val_arr = get_irn_link(irn_blk);
757 n = get_Block_n_cfgpreds(irn_blk);
759 in = alloca(sizeof(*in) * n);
761 for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent))
762 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type < 3)
763 /* This scalar wasn't be saved and we need to produce a phi for it.*/
764 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
766 unk = new_Unknown(mode_M);
767 for (j = n - 1; j >= 0; --j)
770 val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_r_Phi(current_ir_graph, irn_blk, n, in, mode_M);
772 add_ls_to_fixlist(val_arr[GET_IRN_VNUM(leave)].mem_edge_state, GET_IRN_VNUM(leave), env);
775 /* We use for the global memory the phi node, that
776 * is already available.*/
777 val_arr[env->gl_mem_vnum].mem_edge_state = irn;
781 * The function handles the call nodes, that have
782 * as parameter a scalar
784 * @param env The enviroment pinter.
785 * @param call The call node, that must be handled.
786 * @param accessed_entities A set wit all entities, that are accessed from this call node.*/
787 static void split_call_mem_edge(env_t *env, ir_node *call, pset *accessed_entities) {
789 ent_leaves_t key_ent, *value_ent;
790 value_arr_entry_t *val_arr;
791 call_access_t key_call, *value_call;
792 ir_node *call_blk, *new_mem_state, *leave;
796 int fix_irn = 0; /**< Set to 1 if we must add this call to it fix list.*/
797 int *accessed_leaves_vnum = NULL; /**< An arraw, where are saved the value number, that
798 are synced from call's sync node, if we need it.*/
800 if(get_irn_node_nr(call) == 2763)
803 call_blk = get_nodes_block(call);
804 val_arr = get_irn_link(call_blk);
805 /* An array to save the memory edges, that must be
807 in = NEW_ARR_F(ir_node *, 1);
808 /* An array to save the value numbers of the memory
809 * edges that must be repaired.*/
810 accessed_leaves_vnum = NEW_ARR_F(int, 0);
812 /* We get the memory successor of the call node.
813 * It is the new memory state for all synced memory
815 new_mem_state = get_Call_mem_out(call);
817 /* The global memory is the first predecessor of the create sync node.*/
818 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
819 in[0] = new_Unknown(mode_M);
823 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
826 for(ent = pset_first(accessed_entities); ent; ent = pset_next(accessed_entities)) {
827 /* Whit this loop we iterate all accessed entities from this call and collect
828 * all memory edges, that we must sync.*/
829 ent_vnum = GET_ENT_VNUM(ent);
831 key_call.call = call;
832 value_call = set_find(val_arr[ent_vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
835 value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
837 if(val_arr[ent_vnum].access_type <= 3) {
838 /* This scalar's address wasn't stored in this block.*/
839 switch(value_call->access_type) {
841 case ptr_access_none :
842 /* In this case we have nothing to do.*/
845 case ptr_access_read:
846 case ptr_access_write:
848 /* All this cases must be traded equal.*/
850 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
851 /* All this memory edges must be synced.*/
852 add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &accessed_leaves_vnum);
854 /* We update the memory state of this leave.*/
855 if(value_call->access_type != ptr_access_read)
856 val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_mem_state;
865 /* We must update the global memory state.*/
866 val_arr[env->gl_mem_vnum].mem_edge_state = new_mem_state;
868 if(ARR_LEN(in) == 1) {
869 /* we must set the call memory to gobale momory*/
870 set_Call_mem(call,in[0]);
873 /* We add this call node to the call fix list..*/
874 add_ls_to_fixlist(call, env->gl_mem_vnum, env);
877 /* We create the sync and set it as memory predecessor of the call node.*/
878 sync = new_r_Sync(current_ir_graph, call_blk, ARR_LEN(in), in);
879 /* We must check this, why it is possible to get a Bad node
880 * form new_r_Sync(), when the node can be optimized.
881 * In this case we must do nothing.*/
882 if(get_irn_op(sync) == op_Sync) {
884 set_Call_mem(call, sync);
885 if(ARR_LEN(accessed_leaves_vnum))
886 /* We add this sync node to the sync's fix list.*/
887 add_sync_to_fixlist(sync, accessed_leaves_vnum, env);
894 * The function split the memory edge from the passed
895 * ir node if this is needed
897 * @param irn The node, that memory edge must be spleted.
898 * @param env The enviroment pinter.
900 static void split_memory_edge(ir_node *irn, void *ctx) {
903 ir_node *sel, *irn_blk;
905 sels_t key_sels, *value_sels;
906 value_arr_entry_t *val_arr;
907 pset *accessed_entities; /**< A set to save all entities accessed from a call.*/
911 op = get_irn_op(irn);
916 irn_blk = get_nodes_block(irn);
918 if (Block_not_block_visited(irn_blk)) {
919 /* We sync first the stored scalar address in this block.*/
920 mark_Block_block_visited(irn_blk);
921 sync_stored_scalars(irn_blk, env);
924 if(op == op_Load || op == op_Store)
926 split_ls_mem_edge(irn, env);
929 if (op == op_Phi && get_irn_mode(irn) == mode_M) {
931 * found a memory Phi: Here, we must create new Phi nodes
933 split_phi_mem_edge(irn, env);
938 /* Calls that have a NoMem input do neither read nor write memory.
939 We can completely ignore them here. */
940 if (get_irn_op(get_Call_mem(irn)) == op_NoMem)
943 /* We save in this set all entities,
944 * that are accessed from this call node.*/
945 accessed_entities = new_pset(ent_cmp, 8);
946 val_arr = get_irn_link(get_nodes_block(irn));
948 for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
950 sel = get_Call_param(irn, i);
952 if(get_irn_op(sel) == op_Sel) {
954 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
956 if(value_sels != NULL && val_arr[GET_ENT_VNUM(value_sels->ent)].access_type <= 3)
957 /* We save in this set all accessed entities from this call node whit
958 * access none, read, write or rw..*/
959 pset_insert(accessed_entities, value_sels->ent, HASH_PTR(value_sels->ent));
963 if(pset_count(accessed_entities))
964 split_call_mem_edge(env, irn, accessed_entities);
966 del_pset(accessed_entities);
973 * searches through blocks beginning from block for value
974 * vnum and return it.
976 * @param block A block from the current ir graph.
977 * @param vnum The value number, that must be found.
979 static ir_node *find_vnum_value(ir_node *block, unsigned vnum)
981 value_arr_entry_t *val_arr;
985 if (Block_not_block_visited(block)) {
986 mark_Block_block_visited(block);
988 val_arr = get_irn_link(block);
990 if (val_arr[vnum].mem_edge_state)
991 return val_arr[vnum].mem_edge_state;
993 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
994 ir_node *pred = get_Block_cfgpred(block, i);
996 res = find_vnum_value(get_nodes_block(pred), vnum);
1005 * fix the Load/Store or Call list
1007 * @param The enviroment pinter.
1009 static void fix_ls(env_t *env)
1012 ir_node *irn, *block, *pred, *val = NULL;
1016 for (l = env->fix_ls; l; l = get_irn_link(irn)) {
1019 op = get_irn_op(irn);
1020 block = get_nodes_block(irn);
1021 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1022 pred = get_Block_cfgpred(block, i);
1023 pred = get_nodes_block(pred);
1025 inc_irg_block_visited(current_ir_graph);
1026 val = find_vnum_value(pred, l->vnum);
1034 set_Store_mem(irn, val);
1037 set_Load_mem(irn, val);
1039 set_Call_mem(irn, val);
1047 * @param The enviroment pinter.
1049 static void fix_phis(env_t *env)
1052 ir_node *phi, *block, *pred, *val;
1055 for (l = env->fix_phis; l; l = get_irn_link(phi)) {
1058 block = get_nodes_block(phi);
1059 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1061 pred = get_Block_cfgpred(block, i);
1062 pred = get_nodes_block(pred);
1064 inc_irg_block_visited(current_ir_graph);
1065 val = find_vnum_value(pred, l->vnum);
1068 set_irn_n(phi, i, val);
1077 * @param The enviroment pinter.
1079 static void fix_syncs(env_t *env)
1081 syncs_fixlist_entry_t *l;
1082 ir_node *sync, *block, *pred, *val;
1086 for (l = env->fix_syncs; l; l = get_irn_link(sync)) {
1090 /* The sync block must have one predecessor, when it
1091 have unknown nodes as predecessor.*/
1092 block = get_nodes_block(sync);
1093 pred = get_Block_cfgpred(block, 0);
1094 pred = get_nodes_block(pred);
1096 /* We first repair the global memory edge at the first position of sync predecessors.*/
1097 if(get_irn_op(get_irn_n(sync, 0)) == op_Unknown) {
1098 inc_irg_block_visited(current_ir_graph);
1099 val = find_vnum_value(pred, env->gl_mem_vnum);
1102 set_irn_n(sync, 0, val);
1105 for (i = get_irn_arity(sync) - 1; i >= 1; --i) {
1106 /* We repair the leaves*/
1108 assert(k <= ARR_LEN(l->accessed_vnum) && "The algorythm for sync repair is wron");
1109 if(get_irn_op(get_irn_n(sync, i)) == op_Unknown) {
1110 inc_irg_block_visited(current_ir_graph);
1111 val = find_vnum_value(pred, l->accessed_vnum[k++]);
1114 set_irn_n(sync, i, val);
1117 DEL_ARR_F(l->accessed_vnum);
1121 * For the end node we must sync all memory edges.
1123 * @param The enviroment pinter.
1125 static void sync_mem_edges(env_t *env) {
1127 value_arr_entry_t *val_arr;
1128 ir_node **in, *sync, *Return, *Return_blk;
1129 int i, vnum, vnum_state;
1131 Return = get_Block_cfgpred(get_irg_end_block(current_ir_graph), 0);
1132 Return_blk = get_nodes_block(Return);
1133 val_arr = get_irn_link(Return_blk);
1137 for(i = 0; i <= (int)env->gl_mem_vnum; i++)
1138 /* we get the current state of non saved scalars.*/
1139 if(val_arr[i].access_type <= 3)
1142 /* We allocate the memory, that we need for the predecessors of the sync.*/
1143 in = xmalloc(sizeof(ir_node*) *vnum_state);
1145 /* The global memory edge is the first predecessor of this sync node.*/
1146 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
1147 /* We must search through blocks for this memory state.*/
1148 inc_irg_block_visited(current_ir_graph);
1149 in[0] = find_vnum_value(Return_blk, env->gl_mem_vnum);
1152 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
1155 for(i = 1, vnum = 0; vnum < (int)env->gl_mem_vnum; vnum++) {
1157 if(val_arr[vnum].access_type <= 3) {
1158 /* we add the non saved scalars as predecessors of the sync.*/
1160 if(val_arr[vnum].mem_edge_state == NULL) {
1161 /* We must search through blocks for this memory state.*/
1162 inc_irg_block_visited(current_ir_graph);
1163 in[i] = find_vnum_value(Return_blk, vnum);
1166 in[i] = val_arr[vnum].mem_edge_state;
1171 sync = new_r_Sync(current_ir_graph, Return_blk, vnum_state, in);
1172 set_Return_mem(Return, sync);
1178 * Walker: allocate the value array for every block.
1180 * @param block A block from the current ir graph for that must be allocated a value array.
1181 * @param ctx The enviroment pinter.
1183 static void alloc_value_arr(ir_node *block, void *ctx)
1188 value_arr_entry_t *var_arr = obstack_alloc(&env->obst, sizeof(value_arr_entry_t) *(env->nvals + set_count(env->set_ent) + 1));
1190 /* the value array is empty at start */
1191 memset(var_arr, 0, sizeof(value_arr_entry_t) * (env->nvals + set_count(env->set_ent) + 1));
1192 set_irn_link(block, var_arr);
1194 /* We set the block value number state to optimal and later we update this.*/
1195 var_arr[env->vnum_state].access_type = env->nvals;
1197 if(get_irg_start_block(current_ir_graph) == block)
1198 /* We initilize the startblocks array with the irg initilize memory, why
1199 * it must be the start point of all memory edges.*/
1200 for(i = (env->nvals + set_count(env->set_ent)) ; i >=0; i--)
1201 var_arr[i].mem_edge_state = get_irg_initial_mem(current_ir_graph);
1205 /* Analyze call nodes to get information, if they store the address of a scalar.
1207 * @param *irn An ir node from the current_ir_graph.
1208 * @param *env The enviroment pointer.
1210 static void analyse_calls(ir_node *irn, void *ctx) {
1213 unsigned int acces_type;
1214 ir_node *param, *call_ptr, *blk;
1216 ir_entity *meth_ent;
1217 sels_t key_sels, *value_sels;
1218 call_access_t key_call, *value_call;
1219 value_arr_entry_t *val_arr;
1223 if(get_irn_op(irn) != op_Call)
1226 /* Calls that have a NoMem input do neither read nor write memory.
1227 We can completely ignore them here. */
1228 if (get_irn_op(get_Call_mem(irn)) == op_NoMem)
1231 /* We iterate over the parameters of this call nodes.*/
1232 for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
1233 param = get_Call_param(irn, i);
1234 if(get_irn_op(param) == op_Sel) {
1235 /* We have found a parameter with operation sel.*/
1236 key_sels.sel = param;
1237 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
1238 if(value_sels != NULL ) {
1240 /* We have found a call, that have as parameter a sel from our set_sels.*/
1241 call_ptr = get_Call_ptr(irn);
1242 op = get_irn_op(call_ptr);
1244 if(op == op_SymConst && get_SymConst_kind(call_ptr) == symconst_addr_ent) {
1245 meth_ent = get_SymConst_entity(call_ptr);
1246 /* we get the access type for our sel.*/
1247 acces_type = get_method_param_access(meth_ent, i);
1249 /* We can't analyze this function and we asume, that it store the address.*/
1250 acces_type = ptr_access_store;
1252 /* we save the access type and this call in the array allocated for this block.
1253 * The value number of this entity get us the position in the array to save this
1254 * information. Why we expect more calls as one we allocate a set.*/
1255 vnum = GET_ENT_VNUM(value_sels->ent);
1256 blk = get_nodes_block(irn);
1257 val_arr = get_irn_link(blk);
1259 if(val_arr[vnum].access_type > 3)
1260 /* The address of this entity have been stored.*/
1263 if(val_arr[vnum].calls == NULL)
1264 /* for this entity i have found the firs call in this block and we must allocate the set.*/
1265 val_arr[vnum].calls = new_set(call_cmp, 8);
1267 /* This call performs anything with the scalar and we must mark it.*/
1268 key_call.call = irn;
1269 key_call.access_type = acces_type;
1270 value_call = set_insert(val_arr[vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
1272 if(value_call->access_type < acces_type)
1273 /* this case tread, when a call access an entity more at once.
1274 * Than we must save the highest access type.*/
1275 value_call->access_type = acces_type;
1278 /* This call save the address of our scalar and we can't
1279 * use the scalars of this entity for optimization as from now.
1281 val_arr[vnum].access_type = acces_type;
1287 static int set_block_dominated_first_access(ir_node *blk, int vnum, unsigned int access) {
1289 ir_node *idom, *succ;
1290 value_arr_entry_t *val_arr;
1293 idom = get_Block_idom(blk);
1294 for(i = get_Block_n_cfg_outs(idom) - 1; i >=1; i--) {
1295 succ = get_Block_cfg_out(idom, i);
1296 val_arr = get_irn_link(succ);
1297 if(val_arr[vnum].access_type < 3) {
1298 val_arr[vnum].access_type = access;
1304 /* Update the access information of a block if a predecessor of
1305 * this black have a higher access for an entity.
1307 * @param *irn An ir node from the current_ir_graph.
1308 * @param *env The enviroment pointer.
1310 static void set_block_access(ir_node *irn, void *ctx){
1312 value_arr_entry_t *val_arr, *val_arr_pred;
1313 ent_leaves_t *value_leaves;
1314 ir_node *pred, *pred_blk, *leave;
1319 val_arr = get_irn_link(irn);
1321 for( i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
1322 /* We analyze the predecessors of this block to see if this block must
1324 pred = get_Block_cfgpred(irn, i);
1325 pred_blk = get_nodes_block(pred);
1327 val_arr_pred = get_irn_link(pred_blk);
1329 for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1330 vnum = GET_ENT_VNUM(value_leaves->ent);
1332 if((get_Block_n_cfgpreds(irn) > 1) && (val_arr[vnum].access_type > 3))
1333 env->changes = set_block_dominated_first_access(irn, vnum, val_arr[vnum].access_type);
1335 if((val_arr_pred[vnum].access_type > 3) && (val_arr[vnum].access_type < 3)) {
1336 /* We have found a block for update it access and value number information.*/
1337 val_arr[vnum].access_type = val_arr_pred[vnum].access_type;
1338 /* We update the access information of all leave, that belong to
1341 for(leave = pset_first(value_leaves->leaves); leave; leave = pset_next(value_leaves->leaves))
1342 val_arr[GET_IRN_VNUM(leave)].access_type = val_arr[vnum].access_type;
1344 /* In this way can't be got the actuall number of value numbers.
1345 val_arr[env->vnum_state].access_type = val_arr_pred[env->vnum_state].access_type; */
1351 /* Free the allocated call sets.
1353 * @param irn A block form the ir graph.
1354 * @param env The enviroment pinter.
1356 static void free_call_info(ir_node *irn, void *ctx) {
1360 value_arr_entry_t *val_arr;
1363 val_arr = get_irn_link(irn);
1365 for(i = env->nvals + set_count(env->set_ent); i >= 0; i--) {
1366 if(val_arr[i].calls != NULL)
1368 del_set(val_arr[i].calls);
1372 static void print_block_state(ir_node *irn, void *ctx) {
1374 value_arr_entry_t *val_arr;
1375 ent_leaves_t *value_leaves;
1376 call_access_t *value_calls;
1381 val_arr = get_irn_link(irn);
1382 ir_printf("\n\nThe actual value number state of this block is: %i \n",
1383 val_arr[env->vnum_state].access_type - 1);
1385 for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1387 vnum = GET_ENT_VNUM(value_leaves->ent);
1388 ir_printf("The entity %F access type in the block with nr %u is %i \n",
1389 value_leaves->ent, get_irn_node_nr(irn), val_arr[vnum].access_type);
1391 if(val_arr[vnum].calls != NULL)
1392 for(value_calls = set_first(val_arr[vnum].calls); value_calls; value_calls = set_next(val_arr[vnum].calls))
1394 ir_printf("A call with nr %i acess a element of this entity with access %u \n",
1395 get_irn_node_nr(value_calls->call), value_calls->access_type);
1400 /** Optimize the found scalar replacements.
1402 * @param set_sels A set with all entities, that
1404 * @param set_ent A set with all sels nodes,
1405 * that belong to our scalars.
1406 * @param vnum The number of scalars.
1408 static void do_data_flow_scalar_replacement(set *set_ent, set *set_sels, int vnum) {
1412 obstack_init(&env.obst);
1413 env.set_ent = set_ent;
1414 env.set_sels = set_sels;
1416 env.fix_phis = NULL;
1417 env.fix_syncs = NULL;
1418 env.gl_mem_vnum = vnum - 2;
1419 env.vnum_state = vnum - 1;
1420 /* nvals are vnum - 1, why we indicate with nvals the number
1421 * of memory edges we will produce. For vnum_state we don't
1422 * need to produce a memory edge.*/
1423 env.nvals = vnum - 1;
1426 /* first step: allocate the value arrays for every block */
1427 irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
1429 /* second step: we analyze all calls, that have as parameter scalar(s).
1430 * We mark the calls, that save the address of a scalar and we
1431 * mark the entity owner of this scalar as not optimizeble by now.*/
1432 irg_walk_graph(current_ir_graph, NULL, analyse_calls, &env);
1434 while(env.changes) {
1439 * third step: walk over the blocks of a graph and update
1440 * the information for the access of our scalars.
1442 irg_block_walk_graph(current_ir_graph, NULL, set_block_access, &env);
1446 // if(get_firm_verbosity())
1447 /* Debug info to see if analyse_calls work properly.*/
1448 irg_block_walk_graph(current_ir_graph, NULL, print_block_state, &env);
1451 * fourth step: walk over the graph blockwise in topological order
1452 * and split the memrory edge.
1454 inc_irg_block_visited(current_ir_graph);
1455 irg_walk_blkwise_graph(current_ir_graph, NULL, split_memory_edge, &env);
1459 /* fifth step: fix all nodes, that have as predecessor Unknown.*/
1464 /* sixth step: sync memory enges for the end block.*/
1465 sync_mem_edges(&env);
1467 /*seventh step: free the allocated memory*/
1468 irg_block_walk_graph(current_ir_graph, NULL, free_call_info, &env);
1469 obstack_free(&env.obst, NULL);
1473 * Find possible scalar replacements
1475 * @param irg The current ir graph.
1477 void data_flow_scalar_replacement_opt(ir_graph *irg) {
1483 ent_leaves_t key_leaves, *value_leaves;
1486 if (! get_opt_scalar_replacement())
1489 set_sels = new_set(sels_cmp, 8);
1490 set_ent = new_set(ent_leaves_t_cmp, 8);
1492 /* Call algorithm that remove the critical edges of a ir graph. */
1493 remove_critical_cf_edges(irg);
1495 /* Call algorithm that computes the out edges.*/
1496 if (get_irg_outs_state(irg) != outs_consistent)
1497 compute_irg_outs(irg);
1499 /* Call algorithm that computes the loop information.*/
1500 compute_loop_info(irg);
1501 /* Call algorithm that computes the dominance information.*/
1504 /* Find possible scalar replacements */
1505 if (find_possible_replacements(irg)) {
1507 /* Insert in set the scalar replacements. */
1508 irg_frame = get_irg_frame(irg);
1510 for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
1511 ir_node *succ = get_irn_out(irg_frame, i);
1513 if (get_irn_op(succ) == op_Sel) {
1514 ir_entity *ent = get_Sel_entity(succ);
1516 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
1518 /* we have found a entity, that have scalars and we insert it to our set_ent*/
1519 key_leaves.ent = ent;
1520 key_leaves.leaves = new_pset(leave_cmp, 8);
1521 value_leaves = set_insert(set_ent, &key_leaves, sizeof(key_leaves), HASH_PTR(ent));
1523 /* We allocate for every leave sel a vnum.*/
1524 vnum = allocate_value_numbers(set_sels, value_leaves->leaves, ent, vnum);
1528 if(get_firm_verbosity())
1529 printf("vnumber in data flow= %i\n", vnum);
1531 /* Allocate value number for the globule memory edge.
1532 * and a value number for the value numbers state.*/
1535 /* Allocate value numbers for the entities .*/
1536 for(i = vnum,value_leaves = set_first(set_ent); value_leaves; i++, value_leaves = set_next(set_ent))
1537 SET_ENT_VNUM(value_leaves->ent, i);
1540 do_data_flow_scalar_replacement(set_ent, set_sels, vnum);
1542 /*free the allocated memory.*/
1543 for(value_leaves = set_first(set_ent); value_leaves; value_leaves = set_next(set_ent))
1544 del_pset(value_leaves->leaves);