Commit 688d1be8 authored by Matthias Braun's avatar Matthias Braun
Browse files

lower_switch: retain debug info, some smaller cleanups

[r27991]
parent 198718a6
......@@ -23,10 +23,10 @@
* @author Moritz Kroll
* @version $Id$
*/
#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include "array_t.h"
#include "ircons.h"
......@@ -36,13 +36,14 @@
#include "irouts.h"
#include "irpass_t.h"
#include "lowering.h"
#include "error.h"
#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 */
int changed; /**< indicates whether a change was performed */
bool changed; /**< indicates whether a change was performed */
} walk_env_t;
typedef struct case_data {
......@@ -53,7 +54,7 @@ typedef struct case_data {
typedef struct ifcas_env {
ir_node *sel;
int defindex;
ir_node **defusers; /**< the Projs pointing to the default case */
ir_node **defusers; /**< the Projs pointing to the default case */
} ifcas_env_t;
/**
......@@ -63,17 +64,19 @@ typedef struct ifcas_env {
* @param spare_size Allowed spare size for table switches in machine words.
* (Default in edgfe: 128)
*/
static int should_do_table_switch(ir_node *cond, unsigned spare_size)
static bool should_do_table_switch(ir_node *cond, unsigned spare_size)
{
long default_pn;
int i;
ir_node *proj;
long switch_min = LONG_MAX, switch_max = LONG_MIN;
unsigned long spare, num_cases = 0;
long switch_min = LONG_MAX;
long switch_max = LONG_MIN;
unsigned long spare;
unsigned long num_cases = 0;
/* TODO: Minimum size for jump table? */
if (get_irn_n_outs(cond) <= 4)
return 0;
return false;
default_pn = get_Cond_default_proj(cond);
......@@ -114,9 +117,10 @@ static int casecmp(const void *a, const void *b)
/**
* Creates an if cascade realizing binary search.
*/
static void create_if_cascade(ifcas_env_t *env, ir_node *curblock,
case_data_t *curcases, int numcases)
static void create_if_cascade(ifcas_env_t *env, dbg_info *dbgi, ir_node *block,
case_data_t *curcases, unsigned numcases)
{
ir_graph *irg = get_irn_irg(block);
ir_mode *cmp_mode;
ir_node *cmp_sel;
ir_node *sel_block;
......@@ -132,69 +136,71 @@ static void create_if_cascade(ifcas_env_t *env, ir_node *curblock,
* 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))
{
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 == 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);
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);
ir_node *val = new_r_Const_long(irg, cmp_mode, curcases[0].value);
ir_node *cmp = new_rd_Cmp(dbgi, block, cmp_sel, val);
ir_node *proj = new_r_Proj(cmp, mode_b, pn_Cmp_Eq);
ir_node *cond = new_rd_Cond(dbgi, block, proj);
ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
set_Block_cfgpred(curcases[0].target, 0, trueproj);
env->defusers[env->defindex++] = falseproj;
} 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 *val = new_r_Const_long(irg, cmp_mode, curcases[0].value);
ir_node *cmp = new_rd_Cmp(dbgi, block, cmp_sel, val);
ir_node *proj = new_r_Proj(cmp, mode_b, pn_Cmp_Eq);
ir_node *cond = new_rd_Cond(dbgi, block, proj);
ir_node *trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
ir_node *falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
ir_node *in[1];
ir_node *neblock;
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);
set_Block_cfgpred(curcases[0].target, 0, trueproj);
in[0] = falseproj;
neblock = new_r_Block(irg, 1, in);
/* 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);
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);
val = new_r_Const_long(irg, cmp_mode, curcases[1].value);
cmp = new_rd_Cmp(dbgi, neblock, cmp_sel, val);
proj = new_r_Proj(cmp, mode_b, pn_Cmp_Eq);
cond = new_rd_Cond(dbgi, neblock, proj);
trueproj = new_r_Proj(cond, mode_X, pn_Cond_true);
falseproj = new_r_Proj(cond, mode_X, pn_Cond_false);
set_Block_cfgpred(curcases[1].target, 0, trueproj);
env->defusers[env->defindex++] = falseproj;
} else {
/* recursive case: split cases in the middle */
int midcase = numcases / 2;
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 *val = new_r_Const_long(irg, cmp_mode,
curcases[midcase].value);
ir_node *cmp = new_rd_Cmp(dbgi, block, cmp_sel, val);
ir_node *proj = new_r_Proj(cmp, mode_b, pn_Cmp_Lt);
ir_node *cond = new_rd_Cond(dbgi, block, proj);
ir_node *in[1];
ir_node *ltblock;
ir_node *geblock;
in[0] = new_Proj(cond, mode_X, pn_Cond_true);
ltblock = new_Block(1, in);
in[0] = new_r_Proj(cond, mode_X, pn_Cond_true);
ltblock = new_r_Block(irg, 1, in);
set_cur_block(curblock);
in[0] = new_Proj(cond, mode_X, pn_Cond_false);
geblock = new_Block(1, in);
set_cur_block(geblock);
in[0] = new_r_Proj(cond, mode_X, pn_Cond_false);
geblock = new_r_Block(irg, 1, in);
create_if_cascade(env, ltblock, curcases, midcase);
create_if_cascade(env, geblock, curcases + midcase, numcases - midcase);
create_if_cascade(env, dbgi, ltblock, curcases, midcase);
create_if_cascade(env, dbgi, geblock, curcases + midcase,
numcases - midcase);
}
}
......@@ -209,13 +215,18 @@ static void find_cond_nodes(ir_node *block, void *ctx)
ir_node *sel;
ir_mode *sel_mode;
long default_pn;
int i, j = 0, numcases;
int i;
unsigned j = 0;
unsigned numcases;
ir_node *proj;
case_data_t *cases;
ir_node *condblock;
ir_node *defblock = NULL;
dbg_info *dbgi;
ifcas_env_t ifcas_env;
/* because we split critical blocks only blocks with 1 predecessors may
* contain Proj->Cond nodes */
if (get_Block_n_cfgpreds(block) != 1)
return;
......@@ -231,9 +242,17 @@ static void find_cond_nodes(ir_node *block, void *ctx)
sel = get_Cond_selector(cond);
sel_mode = get_irn_mode(sel);
if (sel_mode == mode_b) /* not a switch? */
if (sel_mode == mode_b)
return;
/* the algorithms here don't work reliable for modes bigger than 32
* since we operate with long numbers */
assert(get_mode_size_bits(sel_mode) <= 32);
/* ok, we have found a switch cond */
/* no need to do anything if backend handles out-of-bounds and the switch
* is small enough */
if (should_do_table_switch(cond, env->spare_size))
return;
......@@ -241,20 +260,20 @@ static void find_cond_nodes(ir_node *block, void *ctx)
* 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;
env->changed = true;
numcases = get_irn_n_outs(cond) - 1; // does not contain default case
NEW_ARR_A(case_data_t, cases, numcases);
numcases = get_irn_n_outs(cond) - 1; /* does not contain default case */
cases = XMALLOCN(case_data_t, numcases);
default_pn = get_Cond_default_proj(cond);
ifcas_env.sel = sel;
default_pn = get_Cond_default_proj(cond);
ifcas_env.sel = sel;
ifcas_env.defindex = 0;
NEW_ARR_A(ir_node*, ifcas_env.defusers, numcases);
ifcas_env.defusers = XMALLOCN(ir_node*, numcases);
foreach_out_irn(cond, i, proj) {
long pn = get_Proj_proj(proj);
ir_node *target = get_irn_out(proj, 0);
assert(get_Block_n_cfgpreds(target) == 1 && "Encountered critical edge in switch");
assert(get_Block_n_cfgpreds(target) == 1);
if (pn == default_pn) {
defblock = target;
......@@ -265,35 +284,30 @@ static void find_cond_nodes(ir_node *block, void *ctx)
cases[j].target = target;
j++;
}
assert(j == numcases);
if (defblock == NULL)
panic("Switch %+F has no default proj", cond);
assert(defblock != NULL && "Switch without default proj");
qsort(cases, numcases, sizeof(*cases), casecmp);
qsort(cases, numcases, sizeof(cases[0]), casecmp);
/* Now create the if cascade */
condblock = get_nodes_block(cond);
create_if_cascade(&ifcas_env, condblock, cases, numcases);
dbgi = get_irn_dbg_info(cond);
create_if_cascade(&ifcas_env, dbgi, condblock, cases, numcases);
/* Connect new default case users */
set_irn_in(defblock, ifcas_env.defindex, ifcas_env.defusers);
xfree(cases);
xfree(ifcas_env.defusers);
}
/**
* Lowers all Switches (Cond nodes with non-boolean mode) depending on spare_size.
* They will either remain the same or be converted into if-cascades.
*
* @param irg The ir graph to be lowered.
* @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)
{
walk_env_t env;
ir_graph *rem = current_ir_graph;
current_ir_graph = irg;
env.changed = 0;
env.spare_size = spare_size;
env.changed = false;
env.spare_size = spare_size;
remove_critical_cf_edges(irg);
assure_irg_outs(irg);
......@@ -307,7 +321,6 @@ void lower_switch(ir_graph *irg, unsigned spare_size)
set_irg_extblk_inconsistent(irg);
set_irg_loopinfo_inconsistent(irg);
}
current_ir_graph = rem;
}
struct pass_t {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment