X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fscalar_replace.c;h=8ffa6da3ff60fae3872fb03bd089fcfd7df7a3a5;hb=44fec44bd47ef88ae276179866d4a65cda6e0253;hp=e33f8027fdcf9bd713e2a815040b527e61f00ea1;hpb=4c8850a9b0d3517ca8d547fbdfa0db2ae8f7ee0a;p=libfirm diff --git a/ir/opt/scalar_replace.c b/ir/opt/scalar_replace.c index e33f8027f..8ffa6da3f 100644 --- a/ir/opt/scalar_replace.c +++ b/ir/opt/scalar_replace.c @@ -3,17 +3,48 @@ * File name: ir/opt/scalar_replace.c * Purpose: scalar replacement of arrays and compounds * Author: Beyhan Veliev + * Modified by: Michael Beck * Created: * CVS-ID: $Id$ * Copyright: (c) 1998-2005 Universität Karlsruhe * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_ALLOCA_H +#include +#endif + +#ifdef HAVE_MALLOC_H +#include +#endif + +#ifdef HAVE_STRING_H +#include +#endif #include "scalar_replace.h" +#include "irflag_t.h" +#include "irouts.h" +#include "set.h" +#include "pset.h" +#include "array.h" +#include "tv.h" +#include "ircons_t.h" +#include "hashptr.h" +#include "irgwalk.h" +#include "irgmod.h" +#include "irnode_t.h" +#include "irtools.h" + +#define SET_VNUM(node, vnum) set_irn_link(node, INT_TO_PTR(vnum)) +#define GET_VNUM(node) (unsigned)PTR_TO_INT(get_irn_link(node)) /** - * a path element entry: it is either an entity - * or a tarval, because the evaluate only constant array + * A path element entry: it is either an entity + * or a tarval, because we evaluate only constant array * accesses like a.b.c[8].d */ typedef union { @@ -23,7 +54,7 @@ typedef union { /** * An access path, used to assign value numbers - * to variables that will be skalar replaced + * to variables that will be scalar replaced */ typedef struct _path_t { unsigned vnum; /**< the value number */ @@ -31,41 +62,30 @@ typedef struct _path_t { path_elem_t path[1]; /**< the path */ } path_t; -typedef struct { - ir_node *irn; /**< Phi or unknown node from the graph to be repairing.*/ - ir_node **link; /**< Array of Stores's volue, that have been scalar replaced.*/ -} repairs_t; - -typedef struct _scalars_t{ - entity *ent; /* A entity for scalar replacement.*/ - type *ent_owner; /* The owner of this entity.*/ +typedef struct _scalars_t { + entity *ent; /**< A entity for scalar replacement. */ + ir_type *ent_owner; /**< The owner of this entity. */ } scalars_t; /** - * compare two pathes + * Compare two pathes. + * + * @return 0 if they are identically */ static int path_cmp(const void *elt, const void *key, size_t size) { const path_t *p1 = elt; const path_t *p2 = key; + /* we can use memcmp here, because identical tarvals should have identical addresses */ return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0])); } /** - * Compare two elements of the repairs_t set - */ -static int set_cmp(const void *elt, const void *key, size_t size) -{ - const repairs_t *c1 = elt; - const repairs_t *c2 = key; - - return c1->irn != c2->irn; -} - -/** - * Compare two elements of the scalars_t set + * Compare two elements of the scalars_t set. + * + * @return 0 if they are identically */ static int ent_cmp(const void *elt, const void *key, size_t size) { @@ -76,23 +96,23 @@ static int ent_cmp(const void *elt, const void *key, size_t size) } /** - * a hash for a path + * Calculate a hash value for a path. */ static unsigned path_hash(const path_t *path) { unsigned hash = 0; - int i; + unsigned i; for (i = 0; i < path->path_len; ++i) - hash ^= (unsigned)path->path[i].ent; + hash ^= (unsigned)PTR_TO_INT(path->path[i].ent); return hash >> 4; } /** - * Returns non-zero, if all indeces of a Sel node are constants + * Returns non-zero, if all indeces of a Sel node are constants. * - * @param *sel The Sel node,that muss be chek. + * @param sel the Sel node that will be checked */ static int is_const_sel(ir_node *sel) { int i, n = get_Sel_n_indexs(sel); @@ -106,40 +126,47 @@ static int is_const_sel(ir_node *sel) { return 1; } -/** - * Returns non-zero, if the address of a sel (or it's succsessor sels is taken). - * - * @param sel +/* + * Returns non-zero, if the address of an entity + * represented by a Sel node (or it's successor Sels) is taken. */ -static int is_address_taken(type *ent_type, ir_node *sel) +int is_address_taken(ir_node *sel) { - int i, n; + int i; if (! is_const_sel(sel)) return 1; - n = get_irn_n_outs(sel); - for (i = 0; i < n; ++i) { + for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) { ir_node *succ = get_irn_out(sel, i); switch (get_irn_opcode(succ)) { case iro_Load: + /* ok, we just load from that entity */ break; case iro_Store: - /* check that Sel is not the store's value */ + /* check that Sel is not the Store's value */ if (get_Store_value(succ) == sel) - return 1; + return 1; break; case iro_Sel: { /* Check the Sel successor of Sel */ - int res = is_address_taken(ent_type, succ); + int res = is_address_taken(succ); if (res) return 1; break; } + + case iro_Call: + /* The address of an entity is given as a parameter. + * As long as we do not have analyses that can tell what + * is done with parameters, think is taken. + */ + return 1; + default: /* another op, the address is taken */ return 1; @@ -149,7 +176,7 @@ static int is_address_taken(type *ent_type, ir_node *sel) } /** - * link all leave Sels with the entity + * Link all leave Sels with the entity. * * @param ent the entity that will be scalar replaced * @param sel a Sel node that selects some fields of this entity @@ -167,39 +194,62 @@ static void link_all_leave_sels(entity *ent, ir_node *sel) flag = 0; } } + if (flag) { + /* if Sel nodes with memory inputs are used, a entity can be + * visited more than once causing a ring here, so we use the + * node flag to mark linked nodes + */ + if (irn_visited(sel)) + return; + /* we know we are at a leave, because this function is only * called if the address is NOT taken, so succ must be a Load * or a Store node */ set_irn_link(sel, get_entity_link(ent)); set_entity_link(ent, sel); + + mark_irn_visited(sel); } } +/* we need a special address that serves as an address taken marker */ static char _x; static void *ADDRESS_TAKEN = &_x; /** - * find possible scalar replacements + * Find possible scalar replacements. * * @param irg an IR graph * * This function finds variables on the (members of the) frame type * that can be scalar replaced, because their address is never taken. * If such a variable is found, it's entity link will hold a list of all - * Sel nodes, that selects the atomar fields of this entity. - * Otherwise, the link will be NULL. + * Sel nodes, that selects the atomic fields of this entity. + * Otherwise, the link will be ADDRESS_TAKEN or NULL. + * + * @return non-zero if at least one entity could be replaced + * potentially */ -static void do_find_scalar_replacements(ir_graph *irg) +static int find_possible_replacements(ir_graph *irg) { ir_node *irg_frame = get_irg_frame(irg); int i, n; + int res = 0; + + inc_irg_visited(irg); n = get_irn_n_outs(irg_frame); - /* first, clear the link field of all interesting entities */ - for (i = 0 ; i < n; i++) { + /* + * First, clear the link field of all interesting entities. + * Note that we did not rely on the fact that there is only + * one Sel node per entity, so we might access one entity + * more than once here. + * That's why we have need two loops. + */ + for (i = 0; i < n; ++i) { ir_node *succ = get_irn_out(irg_frame, i); if (get_irn_op(succ) == op_Sel) { @@ -208,37 +258,55 @@ static void do_find_scalar_replacements(ir_graph *irg) } } - /* Check the ir_graph for Sel nodes.If the entity of Sel is't a scalar replacement - set the link of this entity equal NULL. */ - for (i = 0 ; i < n; i++){ + /* + * Check the ir_graph for Sel nodes. If the entity of Sel + * isn't a scalar replacement set the link of this entity + * equal ADDRESS_TAKEN. + */ + for (i = 0; i < n; ++i) { ir_node *succ = get_irn_out(irg_frame, i); if (get_irn_op(succ) == op_Sel) { entity *ent = get_Sel_entity(succ); - type *ent_type = get_entity_type(ent); + ir_type *ent_type; + + if (get_entity_link(ent) == ADDRESS_TAKEN) + continue; + + ent_type = get_entity_type(ent); /* we can handle arrays, structs and atomic types yet */ if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) { - if (is_address_taken(ent_type, succ)) { + if (is_address_taken(succ)) { + if (get_entity_link(ent)) /* killing one */ + --res; set_entity_link(ent, ADDRESS_TAKEN); } - else { - if (get_entity_link(ent) != ADDRESS_TAKEN) - link_all_leave_sels(ent, succ); + else { + /* possible found one */ + if (get_entity_link(ent) == NULL) + ++res; + link_all_leave_sels(ent, succ); } } } } + + return res; } /** - * return a path from the sel node sel to it's root + * Return a path from the Sel node sel to it's root. + * + * @param sel the Sel node + * @param len the length of the path so far */ static path_t *find_path(ir_node *sel, unsigned len) { int pos, i, n; path_t *res; ir_node *pred = get_Sel_ptr(sel); + /* the current Sel node will add some path elements */ n = get_Sel_n_indexs(sel); len += n + 1; @@ -265,9 +333,10 @@ static path_t *find_path(ir_node *sel, unsigned len) /** - * allocate value numbers for the leaves - * in our found entities + * Allocate value numbers for the leaves + * in our found entities. * + * @param sels a set that will contain all Sels that have a value number * @param ent the entity that will be scalar replaced * @param vnum the first value number we can assign * @param modes a flexible array, containing all the modes of @@ -275,7 +344,7 @@ static path_t *find_path(ir_node *sel, unsigned len) * * @return the next free value number */ -static unsigned allocate_value_numbers(entity *ent, unsigned vnum, ir_mode ***modes) +static unsigned allocate_value_numbers(pset *sels, entity *ent, unsigned vnum, ir_mode ***modes) { ir_node *sel, *next; path_t *key, *path; @@ -285,31 +354,41 @@ static unsigned allocate_value_numbers(entity *ent, unsigned vnum, ir_mode ***mo for (sel = get_entity_link(ent); sel; sel = next) { next = get_irn_link(sel); + /* we must mark this sel for later */ + pset_insert_ptr(sels, sel); + key = find_path(sel, 0); path = set_find(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key)); if (path) - set_irn_link(sel, (void *)path->vnum); + SET_VNUM(sel, path->vnum); else { - int i; + unsigned i; key->vnum = vnum++; set_insert(pathes, key, sizeof(*key) + sizeof(key->path[0]) * key->path_len, path_hash(key)); - set_irn_link(sel, (void *)key->vnum); + SET_VNUM(sel, key->vnum); ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15); (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel))); - printf(" %s", get_entity_name(ent)); - for (i = 1; i < key->path_len; ++i) { - if (is_entity(key->path[i].ent)) - printf(".%s", get_entity_name(key->path[i].ent)); - else - printf("[%ld]", get_tarval_long(key->path[i].tv)); + assert((*modes)[key->vnum] && "Value is not atomic"); + +#ifdef DEBUG_libfirm + /* Debug output */ + if (get_opt_scalar_replacement_verbose() && get_firm_verbosity() > 1) { + printf(" %s", get_entity_name(ent)); + for (i = 1; i < key->path_len; ++i) { + if (is_entity(key->path[i].ent)) + printf(".%s", get_entity_name(key->path[i].ent)); + else + printf("[%ld]", get_tarval_long(key->path[i].tv)); + } + printf(" = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])); } - printf(" = %u (%s)\n", (int)get_irn_link(sel), get_mode_name((*modes)[key->vnum])); +#endif /* DEBUG_libfirm */ } free(key); } @@ -319,622 +398,368 @@ static unsigned allocate_value_numbers(entity *ent, unsigned vnum, ir_mode ***mo return vnum; } -static char p; -static void *NODE_VISITED = &p; -static char t; -static void * LOOP_WALK = &t; -static char s; -static void *LOOP_HEAD_PHI = &s; +/** + * A list entry for the fixing lists + */ +typedef struct _list_entry_t { + ir_node *node; /**< the node that must be fixed */ + unsigned vnum; /**< the value number of this node */ +} list_entry_t; /** * environment for memory walker */ typedef struct _env_t { - struct obstack obst; /**< a obstack for the value blocks */ - int nvals; /**< number of values */ - ir_mode **modes; /**< the modes of the values */ + struct obstack obst; /**< a obstack for the value blocks */ + int nvals; /**< number of values */ + ir_mode **modes; /**< the modes of the values */ + list_entry_t *fix_phis; /**< list of all Phi nodes that must be fixed */ + list_entry_t *fix_loads; /**< list of all Load nodes that must be fixed */ + pset *sels; /**< A set of all Sel nodes that have a value number */ } env_t; -static char q; -static void *PRED_SEARCH = &q; - /** - * Recursive walk over the blocks, that are predecessors of "node". - * - * @param *node A Load or Phi node. The predecessor of this node muss be faund. - * @param *value A struct pointer, that contains the block of node and their link. - * @parm *repairs A set, that contains all blocks, that have a array as link, and all Phis, that - * have copies to repair. - * @param pos The position, that muss contain the predecessor of node, in the array, that have got the bloks - * from the repairs set. - * @param phi_pred If node is a Phi, phi_pred is the predecessor number of this Phi, that muss be repair. + * Walker */ - -static void pred_search(ir_node *node, repairs_t *value, set *repairs, int pos, int phi_pred) +static void handle_first(ir_node *node, void *ctx) { - ir_node *nodes_block; - repairs_t key, *value_pred; - DDMN(node); - nodes_block = get_nodes_block(node); - /* If the predecessor is faund. */ - if((pos == -2 && value->link[pos] != NULL)) - return; - - int i, n = get_Block_n_cfgpreds(nodes_block) ; - - for (i = (n - 1); i >= 0; i--){ + env_t *env = ctx; + ir_op *op = get_irn_op(node); + ir_node *adr, *block, *mem, *unk, **value_arr, **in; + unsigned vnum; + int i, j, n; + list_entry_t *l; + + if (op == op_Load) { + /* a load, check if we can resolve it */ + adr = get_Load_ptr(node); + + if (get_irn_op(adr) != op_Sel) + return; - ir_node *pred = get_Block_cfgpred(nodes_block, i); + if (! pset_find_ptr(env->sels, adr)) + return; - key.irn = nodes_block; - value_pred = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - /* If nodes_block haven't the necessary information and the predecessor of it isn't - visited "pred_search" be called rekursive. Else the necessary Information is found - and the recursion braek*/ - if(value_pred == NULL || value_pred->link[pos] == NULL){ - if(get_irn_link(pred) != PRED_SEARCH ){ - set_irn_link(node, PRED_SEARCH); - pred_search(pred, value, repairs, pos, phi_pred); - } - }else{ - if(value->link[pos] == NULL && pos != -2){ - if(get_Block_dom_depth(value->irn) >= - get_Block_dom_depth(get_nodes_block(value_pred->link[pos]))){ - value->link[pos] = value_pred->link[pos]; - pos = -2; - break; - } - }else{ - if(get_irn_op(value->link[pos]) == op_Phi && pos != -2 && - value->link[pos] != value_pred->link[pos]){ - - set_Phi_pred(value->link[pos], phi_pred, value_pred->link[pos]); - pos = -2; - break; - } - } - } - } + /* ok, we have a Load that will be replaced */ + vnum = GET_VNUM(adr); + assert(vnum < (unsigned)env->nvals); - if(get_irn_link(node) == PRED_SEARCH) - set_irn_link(node, NULL); + block = get_nodes_block(node); + value_arr = get_irn_link(block); -} + /* check, if we can replace this Load */ + if (value_arr[vnum]) { + mem = get_Load_mem(node); -/** - * Create a link for blocks, that have Phi, Load or Store nodes for scalar_replacement. - * - * @param *node A Phi, Load or Store node, that block muss get a link. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. - */ -static void block_link(ir_node *node, env_t *env, set *repairs) -{ - repairs_t key, *value; - ir_node *nods_block ; - int i; - nods_block = get_nodes_block(node); - /* If the block of the node haven't a link. */ - if(get_irn_link(nods_block) == NULL){ - DDMN(nods_block); - key.irn = nods_block; - value = set_insert(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - value->link = obstack_alloc(&env->obst, sizeof(ir_node *) * env->nvals); - /* The block of the node be inserted in the set repairs, because it have yet a link. - The link is a array with size equal to number of scalars.*/ - for(i = 0; i < env->nvals; i++) - /* First all links member be set to NULL. */ - value->link[i] = NULL; - set_irn_link(nods_block, value->link); + turn_into_tuple(node, pn_Load_max); + set_Tuple_pred(node, pn_Load_M, mem); + set_Tuple_pred(node, pn_Load_res, value_arr[vnum]); + set_Tuple_pred(node, pn_Load_X_except, new_Bad()); + } + else { + l = obstack_alloc(&env->obst, sizeof(*l)); + l->node = node; + l->vnum = vnum; + set_irn_link(node, env->fix_loads); + env->fix_loads = l; + } } -} + else if (op == op_Store) { + /* a Store always can be replaced */ + adr = get_Store_ptr(node); -/** - * Handle Phis, that get the memory edge of Loads or Stors, that were been scalar replaced or - * will be scalar replaced. - * - * @param *phi A Phi node, that muss have mode_M. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. - * - */ + if (get_irn_op(adr) != op_Sel) + return; -static void phi_handling(ir_node *phi, env_t *env, set *repairs) -{ - ir_node *phi_block, **link; - repairs_t key, *value; + if (! pset_find_ptr(env->sels, adr)) + return; - key.irn = phi; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - /* Test if the phi have been handled. */ - if(value != NULL) - return; + vnum = GET_VNUM(adr); - int rem = get_optimize(); + assert(vnum < (unsigned)env->nvals); - int p = 0, i, phi_preds; - phi_block = get_nodes_block(phi); - phi_preds = get_Phi_n_preds(phi); + block = get_nodes_block(node); + value_arr = get_irn_link(block); - /* Test if the Phi have predecessors, that were been or will be scalar replaced. */ - for(i = 0; i < phi_preds; i++){ - ir_node *pred = get_Phi_pred(phi, i); - ir_node *pred_block = get_nodes_block(pred); + value_arr[vnum] = get_Store_value(node); - key.irn = pred_block; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); + mem = get_Store_mem(node); - if(value != NULL) - p ++; - } - /* Phis from the loop head muss be handled.*/ - if(get_irn_link(phi) == LOOP_HEAD_PHI) - p ++; - - /* If the Phi node have such predecessor(s), be inserted in the repairs set, else is nothing to do - and exit "phi_handling".*/ - if(p){ - key.irn = phi; - value = set_insert(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - }else - return; + turn_into_tuple(node, pn_Store_max); + set_Tuple_pred(node, pn_Store_M, mem); + set_Tuple_pred(node, pn_Store_X_except, new_Bad()); + } + else if (op == op_Phi && get_irn_mode(node) == mode_M) { + /* + * found a memory Phi: Here, we must create new Phi nodes + */ + block = get_nodes_block(node); + value_arr = get_irn_link(block); - //DDMN(phi); - if (get_irn_link(phi_block) == NULL) - block_link(phi, env, repairs); + n = get_Block_n_cfgpreds(block); - link = get_irn_link(phi_block); + in = alloca(sizeof(*in) * n); - ir_node *in[phi_preds] ; - key.irn = phi_block; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(phi_block)); + for (i = env->nvals - 1; i >= 0; --i) { + unk = new_Unknown(env->modes[i]); + for (j = n - 1; j >= 0; --j) + in[j] = unk; - /* We will build soem Phi nodes. Beware, as they have all Unknown predecessors, CSE - * would combine them into one. To prevent this, we must deactivate Optimizations here - */ - for(p = 0; p < env->nvals; p ++) { + value_arr[i] = new_r_Phi(current_ir_graph, block, n, in, env->modes[i]); - for(i = 0; i < phi_preds; i++) - in[i] = new_Unknown(env->modes[p]); + l = obstack_alloc(&env->obst, sizeof(*l)); + l->node = value_arr[i]; + l->vnum = i; - set_optimize(0); - value->link[p] = new_r_Phi(current_ir_graph, get_nodes_block(phi), phi_preds, in, env->modes[p]); - set_optimize(rem); + set_irn_link(value_arr[i], env->fix_phis); + env->fix_phis = l; } -} - -/** - * Handle Loads, that were been scalar replaced or - * will be scalar replaced. - * - * @param *load A load node. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. - * - */ - -static void load_handling(ir_node *load, env_t *env, set *repairs) -{ - repairs_t key, *value; - ir_node * load_ptr, *load_mem, *nods_block; - nods_block = get_nodes_block(load); - load_ptr = get_Load_ptr(load); - load_mem = get_Load_mem(load); - // DDMN(load); - /* The pointer predecessor of Load muss be a Sel node. */ - if(get_irn_op(load_ptr) == op_Sel){ - /* If the link field of sel's entity is set to "ADDRESS_TAKEN", that mean this value - can't be scalar replaced.It is nothing to do and "load_handling" muss be exit.*/ - if ( get_entity_link(get_Sel_entity(load_ptr)) == ADDRESS_TAKEN) - return; - - key.irn = nods_block; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(nods_block)); - /* Load's pointer predecessor's link field contains the position in the block's link, where - muss be searched the predecessor of this load.*/ - unsigned i = (unsigned)get_irn_link(load_ptr); - - /* If the link of Load's block doasn't contains at position "i" a node or isn't calculated, - than muss be called "pred_search".*/ - if (value == NULL){ - block_link(load, env, repairs); - key.irn = nods_block; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(nods_block)); - pred_search(load, value, repairs, i, 0); - }else - if(value->link[i] == NULL) - pred_search(load, value, repairs, i, 0); - - /* If afte "pred_search" call the link of Load's block at position "i" is equal to "NULL", - that means the loading value weren't be initialise and the load predecessor be set to "Unknown"*/ - if(value->link[i] == NULL) - value->link[i] = new_Unknown(env->modes[i]); - /* The load node can be turned to tupel now. The tupel will bi optimized later. */ - turn_into_tuple(load, pn_Load_max); - set_Tuple_pred(load, pn_Load_M, load_mem); - set_Tuple_pred(load, pn_Load_res, value->link[i]); - set_Tuple_pred(load, pn_Load_X_except, new_Bad()); } } /** - * A walker along the memory edge. Load and Phi nodes muss be found for optimisation - * - * @param *node A node from the graph. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. -*/ - -static void memory_edge_walk2(ir_node *node, env_t *env, set *repairs) + * Walker: allocate the value array for every block. + */ +static void alloc_value_arr(ir_node *block, void *ctx) { - int i, p, n = get_irn_arity(node); - repairs_t key, *value, *value_block; - ir_node *phi_pred; - DDMN(node); - - for (i = 0; i < n; i++){ - - ir_node *pred = get_irn_n(node, i); - - if((get_irn_op(pred) == op_Proj && - get_irn_mode(pred) == mode_M) || - get_irn_mode(pred) == mode_T || - is_memop(pred) || - get_irn_op(pred) == op_Call || - get_irn_op(pred) == op_Alloc) - memory_edge_walk2(pred, env, repairs); - - if(get_irn_op(pred) == op_Phi && - get_irn_mode(pred) == mode_M && - get_irn_link(pred) != NODE_VISITED){ - set_irn_link(pred, NODE_VISITED); - memory_edge_walk2(pred, env, repairs); - } - } + env_t *env = ctx; + ir_node **var_arr = obstack_alloc(&env->obst, sizeof(*var_arr) * env->nvals); - if (get_irn_op(node) == op_Load) - load_handling(node, env, repairs); - - if (get_irn_op(node) == op_Phi && get_irn_mode(node) == mode_M){ - key.irn = node; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - /* If the phi is in the set " repairs ", then muss be handled.*/ - if(value != NULL){ - // DDMN(node); - key.irn = get_nodes_block(node); - value_block = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - n = get_irn_arity(node); - /* All predecessors of a Phi node muss be found.*/ - for(i = 0; i < env->nvals; i ++) - for(p = 0; p < n; p ++){ - phi_pred = get_Phi_pred(value->irn, p); - pred_search(phi_pred, value_block, repairs, i, p); - } - } - } - /* Reset the links, that have beeb used by the walk.*/ - if(get_irn_link(node) == NODE_VISITED) - set_irn_link(node, NULL); + /* the value array is empty at start */ + memset(var_arr, 0, sizeof(*var_arr) * env->nvals); + set_irn_link(block, var_arr); } /** - * A walker along the memory edge in a loop.The walker walk from the node to the loop head. - * Load and Phi nodes muss be found for optimisation - * - * @param *node A node from the graph. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. -*/ -static void loop_walk(ir_node *node, env_t *env, set *repairs) + * searches through blocks beginning from block for value + * vnum and return it. + */ +static ir_node *find_value(ir_node *block, unsigned vnum) { - int i, p, n = get_irn_arity(node); - repairs_t key, *value, *value_block; - ir_node *phi_pred; - DDMN(node); - - /* Test if the loop head have been achieved. */ - if(has_backedges(get_nodes_block(node))) - return; - - for (i = 0; i < n; i++){ + ir_node **value_arr; + int i; + ir_node *res; - ir_node *pred = get_irn_n(node, i); + if (Block_not_block_visited(block)) { + mark_Block_block_visited(block); + value_arr = get_irn_link(block); - if((get_irn_op(pred) == op_Proj && - get_irn_mode(pred) == mode_M) || - get_irn_mode(pred) == mode_T || - is_memop(pred) || - get_irn_op(pred) == op_Call || - get_irn_op(pred) == op_Alloc) - loop_walk(pred, env, repairs); + if (value_arr[vnum]) + return value_arr[vnum]; + for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) { + ir_node *pred = get_Block_cfgpred(block, i); - if(get_irn_op(pred) == op_Phi && - get_irn_mode(pred) == mode_M && - get_irn_link(pred) != LOOP_WALK) { - set_irn_link(pred, LOOP_WALK); - loop_walk(pred, env, repairs); + res = find_value(get_nodes_block(pred), vnum); + if (res) + return res; } - } - - - if (get_irn_op(node) == op_Load) - load_handling(node, env, repairs); - - if (get_irn_op(node) == op_Phi && get_irn_mode(node) == mode_M){ - key.irn = node; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - /* If the phi is in the set " repairs ", then muss be handled.*/ - if(value != NULL){ - DDMN(node); - key.irn = get_nodes_block(node); - value_block = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - n = get_irn_arity(node); - /* All predecessors of a Phi node muss be found.*/ - for(i = 0; i < env->nvals; i ++) - for(p = 0; p < n; p ++){ - phi_pred = get_Phi_pred(value->irn, p); - pred_search(phi_pred, value_block, repairs, i, p); - } - } - } - /* Reset the links, that have beeb used by the walk.*/ - if(get_irn_link(node) == LOOP_WALK) - set_irn_link(node, NULL); + return NULL; } /** - * Handle Stors, that were been scalar replaced or - * will be scalar replaced. - * - * @param *load A store node. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. - * + * fix the Phi list */ -static void store_handling(ir_node *store, env_t *env, set *repairs) +static void fix_phis(env_t *env) { - repairs_t key, *value; - ir_node *nods_block, *store_mem, *store_ptr, *store_value, **link, *phi; - ir_loop *store_l; - - /* If the link field of stor's is set to "NODE_VISITED", that mean this value - can't be scalar replaced.It is nothing to do and "load_handling" muss be exit.*/ - - store_ptr = get_Store_ptr(store); - /* The pointer predecessor of Store muss be a Sel node.*/ - if(get_irn_op(store_ptr) == op_Sel) { - - /* If the link field of stor's is set to "ADDRESS_TAKEN", that mean this value - can't be scalar replaced.It's nothing to do and "load_handling" muss be exit.*/ - if ( get_entity_link(get_Sel_entity(store_ptr)) == ADDRESS_TAKEN) - return; - - /* If the Store node is in a loop, than the loop head of the Store - *muss be handled, if thies ist'n be done. */ - store_l = get_irn_loop(store); - if(store_l != NULL){ - phi = get_loop_node(store_l, 0); - if(get_irn_op(phi) != op_Block){ - key.irn = get_nodes_block(phi); - value = set_find(repairs, &key, sizeof(key), HASH_PTR(key.irn)); - if(value == NULL ){ - set_irn_link(phi, LOOP_HEAD_PHI); - block_link(phi, env, repairs); - phi_handling(phi, env, repairs); - } - } - } - - - DDMN(store); - nods_block = get_nodes_block(store); - - key.irn = nods_block; - value = set_find(repairs, &key, sizeof(key), HASH_PTR(nods_block)); - - store_mem = get_Store_mem(store); - store_value = get_Store_value(store); + list_entry_t *l; + ir_node *phi, *block, *pred, *val; + int i; + for (l = env->fix_phis; l; l = get_irn_link(phi)) { + phi = l->node; + block = get_nodes_block(phi); + for (i = get_irn_arity(phi) - 1; i >= 0; --i) { + pred = get_Block_cfgpred(block, i); + pred = get_nodes_block(pred); - if(store_l != NULL) - loop_walk(store, env, repairs); - else - memory_edge_walk2(store, env, repairs); - /* Stor's pointer predecessor's link field contains the position in the block's link, where - muss be saved the value predecessor of this sore.*/ - value->link[(unsigned)get_irn_link(store_ptr)] = store_value; + inc_irg_block_visited(current_ir_graph); + val = find_value(pred, l->vnum); - /* The store node can be turned to tupel now. The tupel will bi optimized later. */ - turn_into_tuple(store, pn_Store_max); - set_Tuple_pred(store, pn_Store_M, store_mem); - set_Tuple_pred(store, pn_Store_X_except, new_Bad()); - - set_irn_link(nods_block, link); + if (val) + set_irn_n(phi, i, val); } + } } /** - * A walker along the memory edge. Stors, Phis and their blocks as well Load's blocks muss be found for optimisation - * - * @param *node A node from the graph. - * @param *env Contains information about scalars number and mode. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. -*/ -static void memory_edge_walk(ir_node *node, env_t *env, set *repairs) + * fix the Load list + */ +static void fix_loads(env_t *env) { - DDMN(node); - if(get_irn_link(get_nodes_block(node)) != NULL) - set_irn_link(get_nodes_block(node), NULL); - int i, n = get_irn_arity(node); + list_entry_t *l; + ir_node *load, *block, *pred, *val, *mem; + int i; - for (i = (n - 1); i >= 0; i--){ + for (l = env->fix_loads; l; l = get_irn_link(load)) { + load = l->node; - ir_node *pred = get_irn_n(node, i); + block = get_nodes_block(load); + for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) { + pred = get_Block_cfgpred(block, i); + pred = get_nodes_block(pred); + inc_irg_block_visited(current_ir_graph); + val = find_value(pred, l->vnum); - if((get_irn_op(pred) == op_Proj && - get_irn_mode(pred) == mode_M) || - is_memop(pred) || - get_irn_op(pred) == op_Call || - get_irn_op(pred) == op_Alloc) - memory_edge_walk(pred, env, repairs); - - - if(get_irn_op(pred) == op_Phi && - get_irn_mode(pred) == mode_M && - get_irn_link(pred) != NODE_VISITED){ - set_irn_link(pred, NODE_VISITED); - memory_edge_walk(pred, env, repairs); + if (val) + break; } - } - - if (get_irn_op(node) == op_Store || - get_irn_op(node) == op_Load) - block_link(node, env, repairs); + if (! val) { + /* access of an uninitialized value */ + val = new_Unknown(env->modes[l->vnum]); + } - if (get_irn_op(node) == op_Phi && get_irn_mode(node) == mode_M) - phi_handling(node, env, repairs); + mem = get_Load_mem(load); - if (get_irn_op(node) == op_Store) - store_handling(node, env, repairs); - /* Reset the links, that have beeb used by the walk.*/ - if(get_irn_link(node) == NODE_VISITED) - set_irn_link(node, NULL); + turn_into_tuple(load, pn_Load_max); + set_Tuple_pred(load, pn_Load_M, mem); + set_Tuple_pred(load, pn_Load_res, val); + set_Tuple_pred(load, pn_Load_X_except, new_Bad()); + } } - - - /** - * Make scalar replacement + * Make scalar replacement. * - * @param *envals The number of scalars. - * @parm *repairs A set, that contains all blocks, that have a link, and all Phis, that - * have copies to repair. - * @param modes A flexible array, containing all the modes of - * the value numbers. + * @param sels A set containing all Sel nodes that have a value number + * @param nvals The number of scalars. + * @param modes A flexible array, containing all the modes of + * the value numbers. */ -static void do_scalar_replacements(int nvals, set * repairs, ir_mode **modes) +static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes) { - ir_node *end_block = get_irg_end_block(current_ir_graph); - int i; - int n = get_Block_n_cfgpreds(end_block); env_t env; obstack_init(&env.obst); - env.nvals = nvals; - env.modes = modes; - /* Search for scalars.*/ - for(i = 0; i < n; i++){ - ir_node *pred = get_Block_cfgpred(end_block, i); - if(get_irn_op(pred) != op_Block) - memory_edge_walk(pred, &env, repairs); - } - /* Search for predecessors of scalars.*/ - for(i = 0; i < n; i++){ - ir_node *pred = get_Block_cfgpred(end_block, i); - if(get_irn_op(pred) != op_Block) - memory_edge_walk2(pred, &env, repairs); + env.nvals = nvals; + env.modes = modes; + env.fix_phis = NULL; + env.fix_loads = NULL; + env.sels = sels; - } + /* first step: allocate the value arrays for every block */ + irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env); + + /* + * second step: walk over the graph blockwise in topological order + * and fill the array as much as possible. + */ + irg_walk_blkwise_graph(current_ir_graph, NULL, handle_first, &env); + + /* third, fix the list of Phis, then the list of Loads */ + fix_phis(&env); + fix_loads(&env); obstack_free(&env.obst, NULL); } - /* * Find possible scalar replacements * - * @param *irg The current ir graph. + * @param irg The current ir graph. */ -void find_scalar_replacements(ir_graph *irg) +void scalar_replacement_opt(ir_graph *irg) { - unsigned nvals; - int i; + unsigned nvals; + int i; scalars_t key, *value; - ir_node *irg_frame; - ir_mode **modes; - - /* Call algorithm that computes the out edges */ - compute_outs(irg); - /* Call algorithm that computes the loop information */ - construct_backedges(irg); + ir_node *irg_frame; + ir_mode **modes; + set *set_ent; + pset *sels; + ir_type *ent_type; + ir_graph *rem; + + if (! get_opt_scalar_replacement()) + return; - /* Call algorithm that computes the backedges */ - construct_cf_backedges(irg); + rem = current_ir_graph; - /* Call algorithm that computes the dominator trees. */ - compute_doms(irg); + /* Call algorithm that computes the out edges */ + if (get_irg_outs_state(irg) != outs_consistent) + compute_irg_outs(irg); /* Find possible scalar replacements */ - do_find_scalar_replacements(irg); - - printf("Called for %s\n", get_entity_name(get_irg_entity(irg))); + if (find_possible_replacements(irg)) { - /* Insert in set the scalar replacemnts. */ - irg_frame = get_irg_frame(irg); - nvals = 0; - modes = NEW_ARR_F(ir_mode *, 16); - set *set_ent = new_set(ent_cmp, 8); - - for (i = 0 ; i < get_irn_n_outs(irg_frame); i++){ - ir_node *succ = get_irn_out(irg_frame, i); + if (get_opt_scalar_replacement_verbose()) { + printf("Scalar Replacement: %s\n", get_entity_name(get_irg_entity(irg))); + } - if( get_irn_op(succ) == op_Sel) { - entity *ent = get_Sel_entity(succ); + /* Insert in set the scalar replacements. */ + irg_frame = get_irg_frame(irg); + nvals = 0; + modes = NEW_ARR_F(ir_mode *, 16); + set_ent = new_set(ent_cmp, 8); + sels = pset_new_ptr(8); + + for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) { + ir_node *succ = get_irn_out(irg_frame, i); + + if (get_irn_op(succ) == op_Sel) { + entity *ent = get_Sel_entity(succ); + + if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN) + continue; + + ent_type = get_entity_type(ent); + + key.ent = ent; + key.ent_owner = get_entity_owner(ent); + set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent)); + + if (get_opt_scalar_replacement_verbose()) { + if (is_Array_type(ent_type)) { + printf(" found array %s\n", get_entity_name(ent)); + } + else if (is_Struct_type(ent_type)) { + printf(" found struct %s\n", get_entity_name(ent)); + } + else if (is_atomic_type(ent_type)) + printf(" found atomic value %s\n", get_entity_name(ent)); + else { + assert(0 && "Neither an array nor a struct or atomic value"); + } + } - if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN) - continue; + nvals = allocate_value_numbers(sels, ent, nvals, &modes); + } + } - type *ent_type = get_entity_type(ent); + if (get_opt_scalar_replacement_verbose()) { + printf(" %u values will be needed\n", nvals); + } - key.ent = ent; - key.ent_owner = get_entity_owner (ent); - set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent)); + /* If scalars were found. */ + if (nvals) { + do_scalar_replacements(sels, nvals, modes); - if (is_Array_type(ent_type)) { - printf("<<<<<<ent_owner, value->ent); } + } - nvals = allocate_value_numbers(ent, nvals, &modes); + del_pset(sels); + del_set(set_ent); + DEL_ARR_F(modes); + + if (nvals) { + /* + * We changed the graph, but did NOT introduce new blocks + * neither changed control flow, cf-backedges should be still + * consistent. + */ + set_irg_outs_inconsistent(irg); + set_irg_loopinfo_inconsistent(irg); } } - printf(" %u values will be needed\n", nvals); - - /* A set for nodes that muss be repair. This are phi or unknown nodes. */ - set *repairs = new_set(set_cmp, 8); - - /* If scalars ware found. */ - if(nvals) - do_scalar_replacements(nvals, repairs, modes); - - value = set_first(set_ent); - for (; value != NULL; value = set_next(set_ent)) - remove_class_member (value->ent_owner, value->ent); - - del_set(set_ent); - DEL_ARR_F(modes); + current_ir_graph = rem; }