X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Flower%2Flower_switch.c;h=d12190a86f252b9e52635b15315a8677d6ff2fe3;hb=9e9c84725fd3fcfbcb819d6d6b88f8da91f121a9;hp=21da18ecc7c5e63bf61289be2ac74ada56a96004;hpb=ef5d655e5e5faedc19885fd7741720510ac778bb;p=libfirm diff --git a/ir/lower/lower_switch.c b/ir/lower/lower_switch.c index 21da18ecc..d12190a86 100644 --- a/ir/lower/lower_switch.c +++ b/ir/lower/lower_switch.c @@ -24,21 +24,23 @@ * @version $Id$ */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif +#include + +#include "array_t.h" #include "ircons.h" +#include "irgopt.h" #include "irgwalk.h" #include "irnode_t.h" #include "irouts.h" +#include "irpass_t.h" -#define foreach_out_irn(irn, i, outirn) for(i = get_irn_n_outs(irn) - 1;\ +#define foreach_out_irn(irn, i, outirn) for (i = get_irn_n_outs(irn) - 1;\ i >= 0 && (outirn = get_irn_out(irn, i)); --i) typedef struct walk_env { - unsigned spare_size; /**< the allowed spare size for table switches */ - struct obstack obst; /**< the obstack where data is allocated on */ + unsigned spare_size; /**< the allowed spare size for table switches */ int changed; /**< indicates whether a change was performed */ } walk_env_t; @@ -47,46 +49,11 @@ typedef struct case_data { ir_node *target; } case_data_t; -/** - * Add the new predecessor x to node node, which is either a Block or a Phi - */ -static void add_pred(ir_node* node, ir_node* x) -{ - ir_node** ins; - int n; - int i; - - assert(is_Block(node) || is_Phi(node)); - - n = get_irn_arity(node); - NEW_ARR_A(ir_node*, ins, n + 1); - for (i = 0; i < n; i++) - ins[i] = get_irn_n(node, i); - ins[n] = x; - set_irn_in(node, n + 1, ins); -} - -/** - * Remove the predecessor x from node node one time, which is either a Block or a Phi - */ -static void remove_pred(ir_node* node, ir_node* x) -{ - ir_node** ins; - int n; - int i, j; - - assert(is_Block(node) || is_Phi(node)); - - n = get_irn_arity(node); - NEW_ARR_A(ir_node*, ins, n - 1); - for (i = 0, j = -1; i < n - 1; i++) - { - ins[++j] = get_irn_n(node, i); - if(ins[i] == x) j--; - } - assert(i == j + 1 && "x is not a pred of node"); - set_irn_in(node, n - 1, ins); -} +typedef struct ifcas_env { + ir_node *sel; + int defindex; + ir_node **defusers; /**< the Projs pointing to the default case */ +} ifcas_env_t; /** * Evaluate a switch and decide whether we should build a table switch. @@ -104,21 +71,21 @@ static int should_do_table_switch(ir_node *cond, unsigned spare_size) unsigned long spare, num_cases = 0; /* TODO: Minimum size for jump table? */ - if(get_irn_n_outs(cond) <= 4) + if (get_irn_n_outs(cond) <= 4) return 0; - default_pn = get_Cond_defaultProj(cond); + default_pn = get_Cond_default_proj(cond); foreach_out_irn(cond, i, proj) { long pn = get_Proj_proj(proj); - if(pn == default_pn) + if (pn == default_pn) continue; - if(pn < switch_min) + if (pn < switch_min) switch_min = pn; - if(pn > switch_max) + if (pn > switch_max) switch_max = pn; - num_cases++; + ++num_cases; } /* @@ -131,53 +98,86 @@ static int should_do_table_switch(ir_node *cond, unsigned spare_size) static int casecmp(const void *a, const void *b) { - return ((case_data_t *) a)->value - ((case_data_t *) b)->value; + const case_data_t *cda = a; + const case_data_t *cdb = b; + + /* + * Enforce unsigned sorting. Signed comparison will behave differently for + * 32-bit values, depending on sizeof(long). This will make the resulting + * array deterministic. + */ + return ((unsigned long)cda->value > (unsigned long)cdb->value) - + ((unsigned long)cda->value < (unsigned long)cdb->value); } /** * Creates an if cascade realizing binary search. */ -static void create_if_cascade(ir_node *sel, ir_node *curblock, ir_node *defblock, - case_data_t *curcases, int numcases) +static void create_if_cascade(ifcas_env_t *env, ir_node *curblock, + case_data_t *curcases, int numcases) { + ir_mode *cmp_mode; + ir_node *cmp_sel; + ir_node *sel_block; + + /* Get the mode and sel node for the comparison. */ + cmp_mode = get_irn_mode(env->sel); + cmp_sel = env->sel; + sel_block = get_nodes_block(cmp_sel); + + /* + * Make sure that an unsigned comparison is used, by converting the sel + * node to an unsigned mode and using that mode for the constants, too. + * This is important, because the qsort applied to the case labels uses + * an unsigned comparison and both comparison methods have to match. + */ + if (mode_is_signed(cmp_mode)) + { + cmp_mode = find_unsigned_mode(cmp_mode); + cmp_sel = new_r_Conv(sel_block, cmp_sel, cmp_mode); + } + + assert(numcases >= 0); + set_cur_block(curblock); - if(numcases == 1) - { - /* only one case: "if(sel == val) goto target else goto default;" */ - ir_node *val = new_Const_long(get_irn_mode(sel), curcases[0].value); - ir_node *cmp = new_Cmp(sel, val); + if (numcases == 0) { + /* zero cases: "goto default;" */ + env->defusers[env->defindex++] = new_Jmp(); + } else if (numcases == 1) { + /* only one case: "if (sel == val) goto target else goto default;" */ + ir_node *val = new_Const_long(cmp_mode, curcases[0].value); + ir_node *cmp = new_Cmp(cmp_sel, val); ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq); ir_node *cond = new_Cond(proj); - add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true)); - add_pred(defblock, new_Proj(cond, mode_X, pn_Cond_false)); - return; - } else if(numcases == 2) { - /* only two cases: "if(sel == val[0]) goto target[0];" */ - ir_node *val = new_Const_long(get_irn_mode(sel), curcases[0].value); - ir_node *cmp = new_Cmp(sel, val); + set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true)); + env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false); + } else if (numcases == 2) { + /* only two cases: "if (sel == val[0]) goto target[0];" */ + ir_node *val = new_Const_long(cmp_mode, curcases[0].value); + ir_node *cmp = new_Cmp(cmp_sel, val); ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Eq); ir_node *cond = new_Cond(proj); ir_node *in[1]; ir_node *neblock; - add_pred(curcases[0].target, new_Proj(cond, mode_X, pn_Cond_true)); + set_Block_cfgpred(curcases[0].target, 0, new_Proj(cond, mode_X, pn_Cond_true)); in[0] = new_Proj(cond, mode_X, pn_Cond_false); neblock = new_Block(1, in); + set_cur_block(neblock); - /* second part: "else if(sel == val[1]) goto target[1] else goto default;" */ - val = new_Const_long(get_irn_mode(sel), curcases[1].value); - cmp = new_Cmp(sel, val); + /* second part: "else if (sel == val[1]) goto target[1] else goto default;" */ + val = new_Const_long(cmp_mode, curcases[1].value); + cmp = new_Cmp(cmp_sel, val); proj = new_Proj(cmp, mode_b, pn_Cmp_Eq); cond = new_Cond(proj); - add_pred(curcases[1].target, new_Proj(cond, mode_X, pn_Cond_true)); - add_pred(defblock, new_Proj(cond, mode_X, pn_Cond_false)); - return; + set_Block_cfgpred(curcases[1].target, 0, new_Proj(cond, mode_X, pn_Cond_true)); + env->defusers[env->defindex++] = new_Proj(cond, mode_X, pn_Cond_false); } else { /* recursive case: split cases in the middle */ int midcase = numcases / 2; - ir_node *val = new_Const_long(get_irn_mode(sel), curcases[midcase].value); - ir_node *cmp = new_Cmp(sel, val); + ir_node *val = new_Const_long(cmp_mode, curcases[midcase].value); + ir_node *cmp = new_Cmp(cmp_sel, val); ir_node *proj = new_Proj(cmp, mode_b, pn_Cmp_Lt); ir_node *cond = new_Cond(proj); ir_node *in[1]; @@ -190,9 +190,10 @@ static void create_if_cascade(ir_node *sel, ir_node *curblock, ir_node *defblock set_cur_block(curblock); in[0] = new_Proj(cond, mode_X, pn_Cond_false); geblock = new_Block(1, in); + set_cur_block(geblock); - create_if_cascade(sel, ltblock, defblock, curcases, midcase); - create_if_cascade(sel, geblock, defblock, curcases + midcase, numcases - midcase); + create_if_cascade(env, ltblock, curcases, midcase); + create_if_cascade(env, geblock, curcases + midcase, numcases - midcase); } } @@ -212,60 +213,67 @@ static void find_cond_nodes(ir_node *block, void *ctx) case_data_t *cases; ir_node *condblock; ir_node *defblock = NULL; + ifcas_env_t ifcas_env; - if(get_Block_n_cfgpreds(block) != 1) + if (get_Block_n_cfgpreds(block) != 1) return; projx = get_Block_cfgpred(block, 0); - if(!is_Proj(projx)) + if (!is_Proj(projx)) return; assert(get_irn_mode(projx) == mode_X); cond = get_Proj_pred(projx); - if(!is_Cond(cond)) + if (!is_Cond(cond)) return; sel = get_Cond_selector(cond); sel_mode = get_irn_mode(sel); - if(sel_mode == mode_b) /* not a switch? */ + if (sel_mode == mode_b) /* not a switch? */ return; - if(should_do_table_switch(cond, env->spare_size)) + if (should_do_table_switch(cond, env->spare_size)) return; /* * Switch should be transformed into an if cascade. * So first order the cases, so we can do a binary search on them. */ + env->changed = 1; numcases = get_irn_n_outs(cond) - 1; // does not contain default case - cases = obstack_alloc(&env->obst, numcases * sizeof(*cases)); + NEW_ARR_A(case_data_t, cases, numcases); - default_pn = get_Cond_defaultProj(cond); + default_pn = get_Cond_default_proj(cond); + ifcas_env.sel = sel; + ifcas_env.defindex = 0; + NEW_ARR_A(ir_node*, ifcas_env.defusers, numcases); foreach_out_irn(cond, i, proj) { long pn = get_Proj_proj(proj); - if(pn == default_pn) - { - defblock = get_irn_out(proj, 0); - remove_pred(defblock, proj); + ir_node *target = get_irn_out(proj, 0); + assert(get_Block_n_cfgpreds(target) == 1 && "Encountered critical edge in switch"); + + if (pn == default_pn) { + defblock = target; continue; } cases[j].value = pn; - cases[j].target = get_irn_out(proj, 0); - remove_pred(cases[j].target, proj); + cases[j].target = target; j++; } + assert(defblock != NULL && "Switch without default proj"); qsort(cases, numcases, sizeof(*cases), casecmp); /* Now create the if cascade */ condblock = get_nodes_block(cond); - create_if_cascade(sel, condblock, defblock, cases, numcases); + create_if_cascade(&ifcas_env, condblock, cases, numcases); - obstack_free(&env->obst, cases); + /* Connect new default case users */ + set_irn_in(defblock, ifcas_env.defindex, ifcas_env.defusers); } /** @@ -276,28 +284,53 @@ static void find_cond_nodes(ir_node *block, void *ctx) * @param spare_size Allowed spare size for table switches in machine words. * (Default in edgfe: 128) */ -void lower_Switch(ir_graph *irg, unsigned spare_size) +void lower_switch(ir_graph *irg, unsigned spare_size) { walk_env_t env; ir_graph *rem = current_ir_graph; current_ir_graph = irg; - obstack_init(&env.obst); + env.changed = 0; env.spare_size = spare_size; + remove_critical_cf_edges(irg); assure_irg_outs(irg); irg_block_walk_graph(irg, find_cond_nodes, NULL, &env); - if(env.changed) { + if (env.changed) { /* control flow changed */ set_irg_outs_inconsistent(irg); set_irg_doms_inconsistent(irg); set_irg_extblk_inconsistent(irg); set_irg_loopinfo_inconsistent(irg); } - - obstack_free(&env.obst, NULL); current_ir_graph = rem; } + +struct pass_t { + ir_graph_pass_t pass; + unsigned spare_size; +}; + +/** + * Wrapper for running lower_switch() as a pass. + */ +static int pass_wrapper(ir_graph *irg, void *context) +{ + struct pass_t *pass = context; + + lower_switch(irg, pass->spare_size); + return 0; +} + +/* creates a pass for lower_switch */ +ir_graph_pass_t *lower_switch_pass(const char *name, unsigned spare_size) +{ + struct pass_t *pass = XMALLOCZ(struct pass_t); + + pass->spare_size = spare_size; + return def_graph_pass_constructor( + &pass->pass, name ? name : "lower_switch", pass_wrapper); +}