2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief scalar replacement of arrays and compounds
23 * @author Beyhan Veliev, Michael Beck
30 #include "iroptimize.h"
44 #include "analyze_irg_args.h"
49 #define SET_ENT_VNUM(ent, vnum) set_entity_link(ent, INT_TO_PTR(vnum))
50 #define GET_ENT_VNUM(ent) (unsigned)PTR_TO_INT(get_entity_link(ent))
51 #define SET_IRN_VNUM(irn, vnum) set_irn_link(irn, INT_TO_PTR(vnum))
52 #define GET_IRN_VNUM(irn) (unsigned)PTR_TO_INT(get_irn_link(irn))
56 typedef struct _ent_leaves_t{
57 ir_entity *ent; /**< An entity, that contains scalars for replace.*/
58 pset *leaves; /**< All leaves of this entity.*/
61 typedef struct _sels_t {
62 ir_node *sel; /**< A sel node, thats entity have scalars.*/
63 ir_entity *ent; /**< The entity of this sel node.*/
66 typedef struct _call_access_t {
67 ir_node *call; /**< A call node, that have as parameter a scalar.*/
68 unsigned int access_type; /**< The access type, with that this call access this scalar.*/
71 typedef struct _fixlist_entry_t {
72 ir_node *irn; /**< An ir node, that must be fixed.*/
73 unsigned int vnum; /**< The value number, that must became this ir node.*/
76 typedef struct _syncs_fixlist_entry_t {
77 ir_node *irn; /**< A sync node that must be fixed.*/
78 int *accessed_vnum; /**< A pointer to save an array with value numbers, that must became this sync.*/
79 }syncs_fixlist_entry_t;
81 /* A entry, that save the memory
82 * edge state and the access state for this leave
83 * int the array,that is created for every block.*/
84 typedef struct _leave_t {
85 ir_node *mem_edge_state; /**< memory state for this scalar in this block.*/
86 unsigned int access_type; /**< access state for this scalar in this block.*/
87 set *calls; /**< call nodes,that change this scalar in this block.*/
91 * A path element entry: it is either an entity
92 * or a tarval, because we evaluate only constant array
93 * accesses like a.b.c[8].d
101 * An access path, used to assign value numbers
102 * to variables that will be scalar replaced
104 typedef struct _path_t {
105 unsigned vnum; /**< the value number */
106 unsigned path_len; /**< the length of the access path */
107 path_elem_t path[1]; /**< the path */
111 * environment for memory walker
113 typedef struct _env_t {
114 struct obstack obst; /**< a obstack for the memory edge */
115 set *set_sels; /**< a set with all sels, that are reachable from an entity with a scalar.*/
116 set *set_ent; /**< a set with all entities that have one or more scalars.*/
117 fixlist_entry_t *fix_phis; /**< list of all Phi nodes that must be fixed */
118 fixlist_entry_t *fix_ls; /**< list of all Load or Store nodes that must be fixed */
119 syncs_fixlist_entry_t *fix_syncs; /**< list of all Sync nodes that must be fixed */
120 unsigned int nvals; /**< to save the number of scalars.*/
121 unsigned int gl_mem_vnum; /**< indicate the position of the globule memory edge state in var_arr.*/
122 unsigned int vnum_state; /**< indicate the position of the value number state in var_arr.*/
123 unsigned int changes; /**< to save if by anlyse_calls is changed anything.*/
129 * Compare two elements of the ent_leaves_t set.
131 * @return 0 if they are identically
133 static int ent_leaves_t_cmp(const void *elt, const void *key, size_t size)
135 const ent_leaves_t *c1 = elt;
136 const ent_leaves_t *c2 = key;
139 return c1->ent != c2->ent;
143 * Compare two elements of the ent_access_t set.
145 * @return 0 if they are identically
147 static int ent_cmp(const void *elt, const void *key)
149 const ir_entity *c1 = elt;
150 const ir_entity *c2 = key;
156 * Compare two elements of the sels_t set.
158 * @return 0 if they are identically
160 static int sels_cmp(const void *elt, const void *key, size_t size)
162 const sels_t *c1 = elt;
163 const sels_t *c2 = key;
166 return c1->sel != c2->sel;
170 * Compare two elements of the leave_t set.
172 * @return 0 if they are identically
174 static int leave_cmp(const void *elt, const void *key)
176 ir_node *c1 = (ir_node *)elt;
177 ir_node *c2 = (ir_node *)key;
179 return get_Sel_entity(c1) != get_Sel_entity(c2);
183 * Compare two elements of the call_access_t set.
185 * @return 0 if they are identically
187 static int call_cmp(const void *elt, const void *key, size_t size)
189 const call_access_t *c1 = elt;
190 const call_access_t *c2 = key;
193 return c1->call != c2->call;
199 * @return 0 if they are identically
201 static int path_cmp(const void *elt, const void *key, size_t size)
203 const path_t *p1 = elt;
204 const path_t *p2 = key;
207 /* we can use memcmp here, because identical tarvals should have identical addresses */
208 return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
212 * Calculate a hash value for a path.
214 static unsigned path_hash(const path_t *path)
219 for (i = 0; i < path->path_len; ++i)
220 hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
226 * Returns non-zero, if all induces of a Sel node are constants.
228 * @param sel the Sel node that will be checked
230 static int is_const_sel(ir_node *sel) {
231 int i, n = get_Sel_n_indexs(sel);
233 for (i = 0; i < n; ++i) {
234 ir_node *idx = get_Sel_index(sel, i);
236 if (get_irn_op(idx) != op_Const)
243 * Returns non-zero, if the address of an entity
244 * represented by a Sel node (or it's successor Sels) is taken.
246 static int is_address_taken_2(ir_node *sel)
250 if (! is_const_sel(sel))
253 for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
254 ir_node *succ = get_irn_out(sel, i);
256 switch (get_irn_opcode(succ)) {
258 /* ok, we just load from that entity */
262 /* check that Sel is not the Store's value */
263 if (get_Store_value(succ) == sel)
268 /* Check the Sel successor of Sel */
269 int res = is_address_taken_2(succ);
277 /* The address of an entity is given as a parameter.
278 * We analyzes that later and optimizes this scalar
284 /* another op, the address is taken */
292 * Link all Sels with the entity.
294 * @param ent the entity that will be scalar replaced
295 * @param sel a Sel node that selects some fields of this entity
297 static void link_all_leave_sels(ir_entity *ent, ir_node *sel)
301 n = get_irn_n_outs(sel);
302 for (i = 0; i < n; ++i) {
303 ir_node *succ = get_irn_out(sel, i);
305 if (get_irn_op(succ) == op_Sel)
306 link_all_leave_sels(ent, succ);
310 /* if Sel nodes with memory inputs are used, a entity can be
311 * visited more than once causing a ring here, so we use the
312 * node flag to mark linked nodes
314 if (irn_visited(sel))
318 * we link the sels to the entity.
320 set_irn_link(sel, get_entity_link(ent));
321 set_entity_link(ent, sel);
323 mark_irn_visited(sel);
326 /* we need a special address that serves as an address taken marker */
328 static void *ADDRESS_TAKEN = &_x;
331 * Find possible scalar replacements.
333 * @param irg an IR graph
335 * This function finds variables on the (members of the) frame type
336 * that can be scalar replaced, because their address is never taken.
337 * If such a variable is found, it's entity link will hold a list of all
338 * Sel nodes, that selects anythings of this entity.
339 * Otherwise, the link will be ADDRESS_TAKEN or NULL.
341 * @return non-zero if at least one entity could be replaced
344 static int find_possible_replacements(ir_graph *irg)
346 ir_node *irg_frame = get_irg_frame(irg);
350 inc_irg_visited(irg);
352 n = get_irn_n_outs(irg_frame);
355 * First, clear the link field of all interestingentities.
356 * Note that we did not rely on the fact that there is only
357 * one Sel node per entity, so we might access one entity
358 * more than once here.
359 * That's why we have need two loops.
361 for (i = 0; i < n; ++i) {
362 ir_node *succ = get_irn_out(irg_frame, i);
364 if (get_irn_op(succ) == op_Sel) {
365 ir_entity *ent = get_Sel_entity(succ);
366 set_entity_link(ent, NULL);
371 * Check the ir_graph for Sel nodes. If the entity of Sel
372 * isn't a scalar replacement set the link of this entity
373 * equal ADDRESS_TAKEN.
375 for (i = 0; i < n; ++i) {
376 ir_node *succ = get_irn_out(irg_frame, i);
378 if (get_irn_op(succ) == op_Sel) {
379 ir_entity *ent = get_Sel_entity(succ);
382 if (get_entity_link(ent) == ADDRESS_TAKEN)
386 * Beware: in rare cases even entities on the frame might be
387 * volatile. This might happen if the entity serves as a store
388 * to a value that must survive a exception. Do not optimize
389 * such entities away.
391 if (get_entity_volatility(ent) == volatility_is_volatile) {
392 set_entity_link(ent, ADDRESS_TAKEN);
396 ent_type = get_entity_type(ent);
398 /* we can handle arrays, structs and atomic types yet */
399 if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
400 if (is_address_taken_2(succ)) {
401 if (get_entity_link(ent)) /* killing one */
403 set_entity_link(ent, ADDRESS_TAKEN);
406 /* possible found one */
407 if (get_entity_link(ent) == NULL)
409 link_all_leave_sels(ent, succ);
418 static int is_leave_sel(ir_node *sel) {
422 for(i = get_irn_n_outs(sel) - 1; i >= 0; i--) {
423 succ = get_irn_out(sel, i);
424 if(get_irn_op(succ) == op_Sel)
432 * Return a path from the Sel node sel to it's root.
434 * @param sel the Sel node
435 * @param len the length of the path so far
437 static path_t *find_path(ir_node *sel, unsigned len)
441 ir_node *pred = get_Sel_ptr(sel);
443 /* the current Sel node will add some path elements */
444 n = get_Sel_n_indexs(sel);
447 if (get_irn_op(pred) != op_Sel) {
448 /* we found the root */
450 res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
454 res = find_path(pred, len);
456 pos = res->path_len - len;
458 res->path[pos++].ent = get_Sel_entity(sel);
459 for (i = 0; i < n; ++i) {
460 ir_node *index = get_Sel_index(sel, i);
462 if(get_irn_op(index) == op_Const)
463 res->path[pos++].tv = get_Const_tarval(index);
469 * Allocate value numbers for the leaves
470 * in our found entities.
472 * @param sels a set that will contain all Sels that have a value number
473 * @param ent the entity that will be scalar replaced
474 * @param vnum the first value number we can assign
475 * @param modes a flexible array, containing all the modes of
478 * @return the next free value number
480 static unsigned allocate_value_numbers(set *set_sels, pset *leaves, ir_entity *ent, unsigned vnum)
485 set *pathes = new_set(path_cmp, 8);
487 /* visit all Sel nodes in the chain of the entity */
488 for (sel = get_entity_link(ent); sel; sel = next) {
489 next = get_irn_link(sel);
491 /* we save for every sel it root entity, why
492 * we need this information, when we split the memory edge,
493 * and we must mark this sel for later. */
496 set_insert(set_sels, &key_sels, sizeof(key_sels), HASH_PTR(sel));
498 if(! is_leave_sel(sel))
500 /* We have found a leave and we add it to the pset of this entity.*/
501 pset_insert(leaves, sel, HASH_PTR(get_Sel_entity(sel)));
503 key = find_path(sel, 0);
504 path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
507 SET_IRN_VNUM(sel, path->vnum);
512 set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key));
514 SET_IRN_VNUM(sel, key->vnum);
520 set_entity_link(ent, NULL);
524 * Add a sync node to it fix list.
526 * @param sync The sync node, that myst be addet to the fix list.
527 * @param unk_vnum An array whit the value number, that are synced with this sync node.
528 * @param env The enviroment pinter.
530 static void add_sync_to_fixlist(ir_node *sync, int *unk_vnum, env_t *env) {
532 syncs_fixlist_entry_t *s;
534 s = obstack_alloc(&env->obst, sizeof(*s));
536 s->accessed_vnum = unk_vnum;
537 set_irn_link(sync, env->fix_syncs);
541 * Add a ir node to it fix list.
543 * @param irn The ir node, that myst be addet to the fix list.
544 * @param vnum The value number, that must baceme this ir node as predecessor later.
545 * @param env The enviroment pinter.
547 static void add_ls_to_fixlist(ir_node *irn, int vnum, env_t *env) {
551 l = obstack_alloc(&env->obst, sizeof(*l));
555 if(get_irn_op(irn) == op_Phi) {
556 set_irn_link(l->irn, env->fix_phis);
559 set_irn_link(l->irn, env->fix_ls);
564 static void add_mem_edge(value_arr_entry_t *val_arr, int vnum, ir_node ***in, int **accessed_vnum) {
566 if(val_arr[vnum].mem_edge_state != NULL)
567 ARR_APP1(ir_node *, *in, val_arr[vnum].mem_edge_state);
569 ARR_APP1(int, *accessed_vnum, vnum);
570 ARR_APP1(ir_node *, *in, new_Unknown(mode_M));
574 * The function handles the scalars, that wase stored
577 * @param blk The block, that must be handled.
578 * @param env The enviroment pinter.
581 /* Return the memory successor of the call node.*/
582 static ir_node *get_Call_mem_out(ir_node *call) {
587 for(i = get_irn_n_outs(call) - 1; i >= 0; i--) {
588 mem = get_irn_out(call, i);
589 if(get_irn_mode(mem) == mode_M)
592 /* is not reachable*/
597 static void sync_stored_scalars(ir_node *blk, env_t *env) {
600 int *unk_vnum; /**< An arraw, where are saved the value number, that
601 are synced from this sync node.*/
602 ent_leaves_t *value_ent;
603 value_arr_entry_t *val_arr_blk, *val_arr;
604 ir_node *pred, *leave, *sync, **in;
605 ir_node *sync_blk; /**< The block, where the sync node must be created.*/
608 val_arr_blk = get_irn_link(blk);
610 for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent)) {
613 if(val_arr_blk[GET_ENT_VNUM(value_ent->ent)].access_type <= 3)
614 /* This entity is not stored in this block.*/
617 for(i = get_Block_n_cfgpreds(blk) - 1; i >= 0; i--) {
619 pred = get_Block_cfgpred(blk, i);
620 pred = get_nodes_block(pred);
621 val_arr = get_irn_link(pred);
623 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type == SYNCED)
624 /* This entity was synced.*/
627 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type <= 3) {
629 /* To avoid repeated sync of this entity in this block.*/
630 val_arr[GET_ENT_VNUM(value_ent->ent)].access_type = SYNCED;
631 /* In this predecessor block is this entity not acessed.
632 * We must sync in the end ot this block.*/
633 if(get_Block_n_cfgpreds(blk) > 1)
634 sync_blk = get_nodes_block(get_Block_cfgpred(blk, i));
638 val_arr = get_irn_link(sync_blk);
639 /* An array to save the memory edges, that must be
641 in = NEW_ARR_F(ir_node *, 1);
643 /* An array to save the value numbers,
644 * that must be repaired.*/
645 unk_vnum = NEW_ARR_F(int, 0);
646 /* The global memory edge.*/
647 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL)
648 in[0] = new_Unknown(mode_M);
650 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
652 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves))
653 /* All this memory edges must be synced.*/
654 add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &unk_vnum);
656 /* We create the sync and set it in the global memory state.*/
657 sync = new_r_Sync(current_ir_graph, sync_blk, ARR_LEN(in), in);
658 /* We must check this, why it is possible to get a Bad node
659 * form new_r_Sync(), when the node can be optimized.
660 * In this case we must do nothing.*/
661 if(get_irn_op(sync) == op_Sync) {
662 val_arr[env->gl_mem_vnum].mem_edge_state = sync;
663 /* We add this sync node to the sync's fix list.*/
664 add_sync_to_fixlist(val_arr[env->gl_mem_vnum].mem_edge_state, unk_vnum, env);
672 * The function split the memory edge of load and store nodes, that have
673 * as predecessor a scalar
675 * @param irn The node, that memory edge must be spleted.
676 * @param env The enviroment pinter.
678 static void split_ls_mem_edge(ir_node *irn, env_t *env) {
681 ir_node *leave, *irn_blk, *mem_state, *new_mem_state;
682 unsigned ent_vnum, sel_vnum, i;
683 value_arr_entry_t *val_arr;
684 sels_t key_sels, *value_sels;
685 ent_leaves_t key_ent, *value_ent;
687 op = get_irn_op(irn);
690 key_sels.sel = get_Load_ptr(irn);
692 key_sels.sel = get_Store_ptr(irn);
694 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
696 if(value_sels != NULL) {
697 /* we have found a load or store, that use a sel of our set
698 * and we must split or extend, if the memory edge have been
699 * split for this sel, the memory edge.*/
701 key_ent.ent = value_sels->ent;
702 value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
703 /*To check if the enities set is right filled. */
704 assert(value_ent && " This sel's entity isn't int the entity set.");
706 leave = pset_find(value_ent->leaves, key_sels.sel, HASH_PTR(get_Sel_entity(key_sels.sel)));
707 /*To check if the leaves set is right filled. */
708 assert(leave && "Anything in data_flow_scalar_replacment algorithm is wrong.");
710 ent_vnum = GET_ENT_VNUM(value_ent->ent);
711 sel_vnum = GET_IRN_VNUM(leave);
712 irn_blk = get_nodes_block(irn);
713 val_arr = get_irn_link(irn_blk);
715 if(val_arr[ent_vnum].access_type == 0)
716 /* We have found a scalar, that address is not stored as jet.*/
719 /* This scalar have been stored.*/
720 i = env->gl_mem_vnum;
722 if(val_arr[i].mem_edge_state == NULL) {
723 /* We split now for this sel the memory edge in this block.*/
724 mem_state = new_Unknown(mode_M);
725 /* We must mark this node to fix later*/
726 add_ls_to_fixlist(irn, i, env);
729 /* We have split the memory edge and the current state is saved.*/
730 mem_state = val_arr[i].mem_edge_state;
732 /* We set this Load or Store to the memory edge of this
735 set_Load_mem(irn, mem_state);
737 set_Store_mem(irn, mem_state);
739 /* When we have split or extended the memory edge we must
740 * update the memory_edge_state of this sel*/
741 new_mem_state = get_irn_out(irn, 0);
742 if(get_irn_mode(new_mem_state) == mode_M)
743 val_arr[i].mem_edge_state = new_mem_state;
745 val_arr[i].mem_edge_state = get_irn_out(irn, 1);
750 * The function split the memory edge of phi nodes, that have
751 * as predecessor a scalar
753 * @param irn The phi node, that memory edge must be spleted.
754 * @param env The enviroment pinter.
756 static void split_phi_mem_edge(ir_node *irn, env_t *env) {
758 ir_node *irn_blk, *unk, *leave, **in;
760 ent_leaves_t *value_ent;
761 value_arr_entry_t *val_arr;
763 irn_blk = get_nodes_block(irn);
764 val_arr = get_irn_link(irn_blk);
766 n = get_Block_n_cfgpreds(irn_blk);
768 in = alloca(sizeof(*in) * n);
770 for(value_ent = set_first(env->set_ent); value_ent; value_ent = set_next(env->set_ent))
771 if(val_arr[GET_ENT_VNUM(value_ent->ent)].access_type < 3)
772 /* This scalar wasn't be saved and we need to produce a phi for it.*/
773 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
775 unk = new_Unknown(mode_M);
776 for (j = n - 1; j >= 0; --j)
779 val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_r_Phi(current_ir_graph, irn_blk, n, in, mode_M);
781 add_ls_to_fixlist(val_arr[GET_IRN_VNUM(leave)].mem_edge_state, GET_IRN_VNUM(leave), env);
784 /* We use for the global memory the phi node, that
785 * is already available.*/
786 val_arr[env->gl_mem_vnum].mem_edge_state = irn;
790 * The function handles the call nodes, that have
791 * as parameter a scalar
793 * @param env The enviroment pinter.
794 * @param call The call node, that must be handled.
795 * @param accessed_entities A set wit all entities, that are accessed from this call node.*/
796 static void split_call_mem_edge(env_t *env, ir_node *call, pset *accessed_entities) {
798 ent_leaves_t key_ent, *value_ent;
799 value_arr_entry_t *val_arr;
800 call_access_t key_call, *value_call;
801 ir_node *call_blk, *new_mem_state, *leave;
805 int fix_irn = 0; /**< Set to 1 if we must add this call to it fix list.*/
806 int *accessed_leaves_vnum = NULL; /**< An arraw, where are saved the value number, that
807 are synced from call's sync node, if we need it.*/
809 if(get_irn_node_nr(call) == 2763)
812 call_blk = get_nodes_block(call);
813 val_arr = get_irn_link(call_blk);
814 /* An array to save the memory edges, that must be
816 in = NEW_ARR_F(ir_node *, 1);
817 /* An array to save the value numbers of the memory
818 * edges that must be repaired.*/
819 accessed_leaves_vnum = NEW_ARR_F(int, 0);
821 /* We get the memory successor of the call node.
822 * It is the new memory state for all synced memory
824 new_mem_state = get_Call_mem_out(call);
826 /* The global memory is the first predecessor of the create sync node.*/
827 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
828 in[0] = new_Unknown(mode_M);
832 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
835 for(ent = pset_first(accessed_entities); ent; ent = pset_next(accessed_entities)) {
836 /* Whit this loop we iterate all accessed entities from this call and collect
837 * all memory edges, that we must sync.*/
838 ent_vnum = GET_ENT_VNUM(ent);
840 key_call.call = call;
841 value_call = set_find(val_arr[ent_vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
844 value_ent = set_find(env->set_ent, &key_ent, sizeof(key_ent), HASH_PTR(key_ent.ent));
846 if(val_arr[ent_vnum].access_type <= 3) {
847 /* This scalar's address wasn't stored in this block.*/
848 switch(value_call->access_type) {
850 case ptr_access_none :
851 /* In this case we have nothing to do.*/
854 case ptr_access_read:
855 case ptr_access_write:
857 /* All this cases must be traded equal.*/
859 for(leave = pset_first(value_ent->leaves); leave; leave = pset_next(value_ent->leaves)){
860 /* All this memory edges must be synced.*/
861 add_mem_edge(val_arr, GET_IRN_VNUM(leave), &in, &accessed_leaves_vnum);
863 /* We update the memory state of this leave.*/
864 if(value_call->access_type != ptr_access_read)
865 val_arr[GET_IRN_VNUM(leave)].mem_edge_state = new_mem_state;
874 /* We must update the global memory state.*/
875 val_arr[env->gl_mem_vnum].mem_edge_state = new_mem_state;
877 if(ARR_LEN(in) == 1) {
878 /* we must set the call memory to gobale momory*/
879 set_Call_mem(call,in[0]);
882 /* We add this call node to the call fix list..*/
883 add_ls_to_fixlist(call, env->gl_mem_vnum, env);
886 /* We create the sync and set it as memory predecessor of the call node.*/
887 sync = new_r_Sync(current_ir_graph, call_blk, ARR_LEN(in), in);
888 /* We must check this, why it is possible to get a Bad node
889 * form new_r_Sync(), when the node can be optimized.
890 * In this case we must do nothing.*/
891 if(get_irn_op(sync) == op_Sync) {
893 set_Call_mem(call, sync);
894 if(ARR_LEN(accessed_leaves_vnum))
895 /* We add this sync node to the sync's fix list.*/
896 add_sync_to_fixlist(sync, accessed_leaves_vnum, env);
903 * The function split the memory edge from the passed
904 * ir node if this is needed
906 * @param irn The node, that memory edge must be spleted.
907 * @param env The enviroment pinter.
909 static void split_memory_edge(ir_node *irn, void *ctx) {
912 ir_node *sel, *irn_blk;
914 sels_t key_sels, *value_sels;
915 value_arr_entry_t *val_arr;
916 pset *accessed_entities; /**< A set to save all entities accessed from a call.*/
920 op = get_irn_op(irn);
925 irn_blk = get_nodes_block(irn);
927 if (Block_not_block_visited(irn_blk)) {
928 /* We sync first the stored scalar address in this block.*/
929 mark_Block_block_visited(irn_blk);
930 sync_stored_scalars(irn_blk, env);
933 if(op == op_Load || op == op_Store)
935 split_ls_mem_edge(irn, env);
938 if (op == op_Phi && get_irn_mode(irn) == mode_M) {
940 * found a memory Phi: Here, we must create new Phi nodes
942 split_phi_mem_edge(irn, env);
947 /* Calls that have a NoMem input do neither read nor write memory.
948 We can completely ignore them here. */
949 if (get_irn_op(get_Call_mem(irn)) == op_NoMem)
952 /* We save in this set all entities,
953 * that are accessed from this call node.*/
954 accessed_entities = new_pset(ent_cmp, 8);
955 val_arr = get_irn_link(get_nodes_block(irn));
957 for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
959 sel = get_Call_param(irn, i);
961 if(get_irn_op(sel) == op_Sel) {
963 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
965 if(value_sels != NULL && val_arr[GET_ENT_VNUM(value_sels->ent)].access_type <= 3)
966 /* We save in this set all accessed entities from this call node whit
967 * access none, read, write or rw..*/
968 pset_insert(accessed_entities, value_sels->ent, HASH_PTR(value_sels->ent));
972 if(pset_count(accessed_entities))
973 split_call_mem_edge(env, irn, accessed_entities);
975 del_pset(accessed_entities);
982 * searches through blocks beginning from block for value
983 * vnum and return it.
985 * @param block A block from the current ir graph.
986 * @param vnum The value number, that must be found.
988 static ir_node *find_vnum_value(ir_node *block, unsigned vnum)
990 value_arr_entry_t *val_arr;
994 if (Block_not_block_visited(block)) {
995 mark_Block_block_visited(block);
997 val_arr = get_irn_link(block);
999 if (val_arr[vnum].mem_edge_state)
1000 return val_arr[vnum].mem_edge_state;
1002 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1003 ir_node *pred = get_Block_cfgpred(block, i);
1005 res = find_vnum_value(get_nodes_block(pred), vnum);
1014 * fix the Load/Store or Call list
1016 * @param The enviroment pinter.
1018 static void fix_ls(env_t *env)
1021 ir_node *irn, *block, *pred, *val = NULL;
1025 for (l = env->fix_ls; l; l = get_irn_link(irn)) {
1028 op = get_irn_op(irn);
1029 block = get_nodes_block(irn);
1030 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1031 pred = get_Block_cfgpred(block, i);
1032 pred = get_nodes_block(pred);
1034 inc_irg_block_visited(current_ir_graph);
1035 val = find_vnum_value(pred, l->vnum);
1043 set_Store_mem(irn, val);
1046 set_Load_mem(irn, val);
1048 set_Call_mem(irn, val);
1056 * @param The enviroment pinter.
1058 static void fix_phis(env_t *env)
1061 ir_node *phi, *block, *pred, *val;
1064 for (l = env->fix_phis; l; l = get_irn_link(phi)) {
1067 block = get_nodes_block(phi);
1068 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1070 pred = get_Block_cfgpred(block, i);
1071 pred = get_nodes_block(pred);
1073 inc_irg_block_visited(current_ir_graph);
1074 val = find_vnum_value(pred, l->vnum);
1077 set_irn_n(phi, i, val);
1086 * @param The enviroment pinter.
1088 static void fix_syncs(env_t *env)
1090 syncs_fixlist_entry_t *l;
1091 ir_node *sync, *block, *pred, *val;
1095 for (l = env->fix_syncs; l; l = get_irn_link(sync)) {
1099 /* The sync block must have one predecessor, when it
1100 have unknown nodes as predecessor.*/
1101 block = get_nodes_block(sync);
1102 pred = get_Block_cfgpred(block, 0);
1103 pred = get_nodes_block(pred);
1105 /* We first repair the global memory edge at the first position of sync predecessors.*/
1106 if(get_irn_op(get_irn_n(sync, 0)) == op_Unknown) {
1107 inc_irg_block_visited(current_ir_graph);
1108 val = find_vnum_value(pred, env->gl_mem_vnum);
1111 set_irn_n(sync, 0, val);
1114 for (i = get_irn_arity(sync) - 1; i >= 1; --i) {
1115 /* We repair the leaves*/
1117 assert(k <= ARR_LEN(l->accessed_vnum) && "The algorythm for sync repair is wron");
1118 if(get_irn_op(get_irn_n(sync, i)) == op_Unknown) {
1119 inc_irg_block_visited(current_ir_graph);
1120 val = find_vnum_value(pred, l->accessed_vnum[k++]);
1123 set_irn_n(sync, i, val);
1126 DEL_ARR_F(l->accessed_vnum);
1130 * For the end node we must sync all memory edges.
1132 * @param The enviroment pinter.
1134 static void sync_mem_edges(env_t *env) {
1136 value_arr_entry_t *val_arr;
1137 ir_node **in, *sync, *Return, *Return_blk;
1138 int i, vnum, vnum_state;
1140 Return = get_Block_cfgpred(get_irg_end_block(current_ir_graph), 0);
1141 Return_blk = get_nodes_block(Return);
1142 val_arr = get_irn_link(Return_blk);
1146 for(i = 0; i <= (int)env->gl_mem_vnum; i++)
1147 /* we get the current state of non saved scalars.*/
1148 if(val_arr[i].access_type <= 3)
1151 /* We allocate the memory, that we need for the predecessors of the sync.*/
1152 in = xmalloc(sizeof(ir_node*) *vnum_state);
1154 /* The global memory edge is the first predecessor of this sync node.*/
1155 if(val_arr[env->gl_mem_vnum].mem_edge_state == NULL) {
1156 /* We must search through blocks for this memory state.*/
1157 inc_irg_block_visited(current_ir_graph);
1158 in[0] = find_vnum_value(Return_blk, env->gl_mem_vnum);
1161 in[0] = val_arr[env->gl_mem_vnum].mem_edge_state;
1164 for(i = 1, vnum = 0; vnum < (int)env->gl_mem_vnum; vnum++) {
1166 if(val_arr[vnum].access_type <= 3) {
1167 /* we add the non saved scalars as predecessors of the sync.*/
1169 if(val_arr[vnum].mem_edge_state == NULL) {
1170 /* We must search through blocks for this memory state.*/
1171 inc_irg_block_visited(current_ir_graph);
1172 in[i] = find_vnum_value(Return_blk, vnum);
1175 in[i] = val_arr[vnum].mem_edge_state;
1180 sync = new_r_Sync(current_ir_graph, Return_blk, vnum_state, in);
1181 set_Return_mem(Return, sync);
1187 * Walker: allocate the value array for every block.
1189 * @param block A block from the current ir graph for that must be allocated a value array.
1190 * @param ctx The enviroment pinter.
1192 static void alloc_value_arr(ir_node *block, void *ctx)
1197 value_arr_entry_t *var_arr = obstack_alloc(&env->obst, sizeof(value_arr_entry_t) *(env->nvals + set_count(env->set_ent) + 1));
1199 /* the value array is empty at start */
1200 memset(var_arr, 0, sizeof(value_arr_entry_t) * (env->nvals + set_count(env->set_ent) + 1));
1201 set_irn_link(block, var_arr);
1203 /* We set the block value number state to optimal and later we update this.*/
1204 var_arr[env->vnum_state].access_type = env->nvals;
1206 if(get_irg_start_block(current_ir_graph) == block)
1207 /* We initilize the startblocks array with the irg initilize memory, why
1208 * it must be the start point of all memory edges.*/
1209 for(i = (env->nvals + set_count(env->set_ent)) ; i >=0; i--)
1210 var_arr[i].mem_edge_state = get_irg_initial_mem(current_ir_graph);
1214 /* Analyze call nodes to get information, if they store the address of a scalar.
1216 * @param *irn An ir node from the current_ir_graph.
1217 * @param *env The enviroment pointer.
1219 static void analyse_calls(ir_node *irn, void *ctx) {
1222 unsigned int acces_type;
1223 ir_node *param, *call_ptr, *blk;
1225 ir_entity *meth_ent;
1226 sels_t key_sels, *value_sels;
1227 call_access_t key_call, *value_call;
1228 value_arr_entry_t *val_arr;
1232 if(get_irn_op(irn) != op_Call)
1235 /* Calls that have a NoMem input do neither read nor write memory.
1236 We can completely ignore them here. */
1237 if (get_irn_op(get_Call_mem(irn)) == op_NoMem)
1240 /* We iterate over the parameters of this call nodes.*/
1241 for ( i = get_Call_n_params(irn) - 1; i >= 0; i--) {
1242 param = get_Call_param(irn, i);
1243 if(get_irn_op(param) == op_Sel) {
1244 /* We have found a parameter with operation sel.*/
1245 key_sels.sel = param;
1246 value_sels = set_find(env->set_sels, &key_sels, sizeof(key_sels), HASH_PTR(key_sels.sel));
1247 if(value_sels != NULL ) {
1249 /* We have found a call, that have as parameter a sel from our set_sels.*/
1250 call_ptr = get_Call_ptr(irn);
1251 op = get_irn_op(call_ptr);
1253 if(op == op_SymConst && get_SymConst_kind(call_ptr) == symconst_addr_ent) {
1254 meth_ent = get_SymConst_entity(call_ptr);
1255 /* we get the access type for our sel.*/
1256 acces_type = get_method_param_access(meth_ent, i);
1258 /* We can't analyze this function and we asume, that it store the address.*/
1259 acces_type = ptr_access_store;
1261 /* we save the access type and this call in the array allocated for this block.
1262 * The value number of this entity get us the position in the array to save this
1263 * information. Why we expect more calls as one we allocate a set.*/
1264 vnum = GET_ENT_VNUM(value_sels->ent);
1265 blk = get_nodes_block(irn);
1266 val_arr = get_irn_link(blk);
1268 if(val_arr[vnum].access_type > 3)
1269 /* The address of this entity have been stored.*/
1272 if(val_arr[vnum].calls == NULL)
1273 /* for this entity i have found the firs call in this block and we must allocate the set.*/
1274 val_arr[vnum].calls = new_set(call_cmp, 8);
1276 /* This call performs anything with the scalar and we must mark it.*/
1277 key_call.call = irn;
1278 key_call.access_type = acces_type;
1279 value_call = set_insert(val_arr[vnum].calls, &key_call, sizeof(key_call), HASH_PTR(key_call.call));
1281 if(value_call->access_type < acces_type)
1282 /* this case tread, when a call access an entity more at once.
1283 * Than we must save the highest access type.*/
1284 value_call->access_type = acces_type;
1287 /* This call save the address of our scalar and we can't
1288 * use the scalars of this entity for optimization as from now.
1290 val_arr[vnum].access_type = acces_type;
1296 static int set_block_dominated_first_access(ir_node *blk, int vnum, unsigned int access) {
1298 ir_node *idom, *succ;
1299 value_arr_entry_t *val_arr;
1302 idom = get_Block_idom(blk);
1303 for(i = get_Block_n_cfg_outs(idom) - 1; i >=1; i--) {
1304 succ = get_Block_cfg_out(idom, i);
1305 val_arr = get_irn_link(succ);
1306 if(val_arr[vnum].access_type < 3) {
1307 val_arr[vnum].access_type = access;
1313 /* Update the access information of a block if a predecessor of
1314 * this black have a higher access for an entity.
1316 * @param *irn An ir node from the current_ir_graph.
1317 * @param *env The enviroment pointer.
1319 static void set_block_access(ir_node *irn, void *ctx){
1321 value_arr_entry_t *val_arr, *val_arr_pred;
1322 ent_leaves_t *value_leaves;
1323 ir_node *pred, *pred_blk, *leave;
1328 val_arr = get_irn_link(irn);
1330 for( i = get_Block_n_cfgpreds(irn) - 1; i >= 0; i--) {
1331 /* We analyze the predecessors of this block to see if this block must
1333 pred = get_Block_cfgpred(irn, i);
1334 pred_blk = get_nodes_block(pred);
1336 val_arr_pred = get_irn_link(pred_blk);
1338 for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1339 vnum = GET_ENT_VNUM(value_leaves->ent);
1341 if((get_Block_n_cfgpreds(irn) > 1) && (val_arr[vnum].access_type > 3))
1342 env->changes = set_block_dominated_first_access(irn, vnum, val_arr[vnum].access_type);
1344 if((val_arr_pred[vnum].access_type > 3) && (val_arr[vnum].access_type < 3)) {
1345 /* We have found a block for update it access and value number information.*/
1346 val_arr[vnum].access_type = val_arr_pred[vnum].access_type;
1347 /* We update the access information of all leave, that belong to
1350 for(leave = pset_first(value_leaves->leaves); leave; leave = pset_next(value_leaves->leaves))
1351 val_arr[GET_IRN_VNUM(leave)].access_type = val_arr[vnum].access_type;
1353 /* In this way can't be got the actuall number of value numbers.
1354 val_arr[env->vnum_state].access_type = val_arr_pred[env->vnum_state].access_type; */
1360 /* Free the allocated call sets.
1362 * @param irn A block form the ir graph.
1363 * @param env The enviroment pinter.
1365 static void free_call_info(ir_node *irn, void *ctx) {
1369 value_arr_entry_t *val_arr;
1372 val_arr = get_irn_link(irn);
1374 for(i = env->nvals + set_count(env->set_ent); i >= 0; i--) {
1375 if(val_arr[i].calls != NULL)
1377 del_set(val_arr[i].calls);
1381 static void print_block_state(ir_node *irn, void *ctx) {
1383 value_arr_entry_t *val_arr;
1384 ent_leaves_t *value_leaves;
1385 call_access_t *value_calls;
1390 val_arr = get_irn_link(irn);
1391 ir_printf("\n\nThe actual value number state of this block is: %i \n",
1392 val_arr[env->vnum_state].access_type - 1);
1394 for(value_leaves = set_first(env->set_ent); value_leaves; value_leaves = set_next(env->set_ent)) {
1396 vnum = GET_ENT_VNUM(value_leaves->ent);
1397 ir_printf("The entity %F access type in the block with nr %u is %i \n",
1398 value_leaves->ent, get_irn_node_nr(irn), val_arr[vnum].access_type);
1400 if(val_arr[vnum].calls != NULL)
1401 for(value_calls = set_first(val_arr[vnum].calls); value_calls; value_calls = set_next(val_arr[vnum].calls))
1403 ir_printf("A call with nr %i acess a element of this entity with access %u \n",
1404 get_irn_node_nr(value_calls->call), value_calls->access_type);
1409 /** Optimize the found scalar replacements.
1411 * @param set_sels A set with all entities, that
1413 * @param set_ent A set with all sels nodes,
1414 * that belong to our scalars.
1415 * @param vnum The number of scalars.
1417 static void do_data_flow_scalar_replacement(set *set_ent, set *set_sels, int vnum) {
1421 obstack_init(&env.obst);
1422 env.set_ent = set_ent;
1423 env.set_sels = set_sels;
1425 env.fix_phis = NULL;
1426 env.fix_syncs = NULL;
1427 env.gl_mem_vnum = vnum - 2;
1428 env.vnum_state = vnum - 1;
1429 /* nvals are vnum - 1, why we indicate with nvals the number
1430 * of memory edges we will produce. For vnum_state we don't
1431 * need to produce a memory edge.*/
1432 env.nvals = vnum - 1;
1435 /* first step: allocate the value arrays for every block */
1436 irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
1438 /* second step: we analyze all calls, that have as parameter scalar(s).
1439 * We mark the calls, that save the address of a scalar and we
1440 * mark the entity owner of this scalar as not optimizeble by now.*/
1441 irg_walk_graph(current_ir_graph, NULL, analyse_calls, &env);
1443 while(env.changes) {
1448 * third step: walk over the blocks of a graph and update
1449 * the information for the access of our scalars.
1451 irg_block_walk_graph(current_ir_graph, NULL, set_block_access, &env);
1455 // if(get_firm_verbosity())
1456 /* Debug info to see if analyse_calls work properly.*/
1457 irg_block_walk_graph(current_ir_graph, NULL, print_block_state, &env);
1460 * fourth step: walk over the graph blockwise in topological order
1461 * and split the memrory edge.
1463 inc_irg_block_visited(current_ir_graph);
1464 irg_walk_blkwise_graph(current_ir_graph, NULL, split_memory_edge, &env);
1468 /* fifth step: fix all nodes, that have as predecessor Unknown.*/
1473 /* sixth step: sync memory enges for the end block.*/
1474 sync_mem_edges(&env);
1476 /*seventh step: free the allocated memory*/
1477 irg_block_walk_graph(current_ir_graph, NULL, free_call_info, &env);
1478 obstack_free(&env.obst, NULL);
1482 * Find possible scalar replacements
1484 * @param irg The current ir graph.
1486 void data_flow_scalar_replacement_opt(ir_graph *irg) {
1492 ent_leaves_t key_leaves, *value_leaves;
1495 if (! get_opt_scalar_replacement())
1498 set_sels = new_set(sels_cmp, 8);
1499 set_ent = new_set(ent_leaves_t_cmp, 8);
1501 /* Call algorithm that remove the critical edges of a ir graph. */
1502 remove_critical_cf_edges(irg);
1504 /* Call algorithm that computes the out edges.*/
1505 assure_irg_outs(irg);
1507 /* Call algorithm that computes the loop information.*/
1508 construct_cf_backedges(irg);
1510 /* Call algorithm that computes the dominance information.*/
1513 /* Find possible scalar replacements */
1514 if (find_possible_replacements(irg)) {
1516 /* Insert in set the scalar replacements. */
1517 irg_frame = get_irg_frame(irg);
1519 for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
1520 ir_node *succ = get_irn_out(irg_frame, i);
1522 if (get_irn_op(succ) == op_Sel) {
1523 ir_entity *ent = get_Sel_entity(succ);
1525 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
1527 /* we have found a entity, that have scalars and we insert it to our set_ent*/
1528 key_leaves.ent = ent;
1529 key_leaves.leaves = new_pset(leave_cmp, 8);
1530 value_leaves = set_insert(set_ent, &key_leaves, sizeof(key_leaves), HASH_PTR(ent));
1532 /* We allocate for every leave sel a vnum.*/
1533 vnum = allocate_value_numbers(set_sels, value_leaves->leaves, ent, vnum);
1537 if(get_firm_verbosity())
1538 printf("vnumber in data flow= %i\n", vnum);
1540 /* Allocate value number for the globule memory edge.
1541 * and a value number for the value numbers state.*/
1544 /* Allocate value numbers for the entities .*/
1545 for(i = vnum,value_leaves = set_first(set_ent); value_leaves; i++, value_leaves = set_next(set_ent))
1546 SET_ENT_VNUM(value_leaves->ent, i);
1549 do_data_flow_scalar_replacement(set_ent, set_sels, vnum);
1551 /*free the allocated memory.*/
1552 for(value_leaves = set_first(set_ent); value_leaves; value_leaves = set_next(set_ent))
1553 del_pset(value_leaves->leaves);