static ir_mode *lowered_mode = NULL;
static int lower_direct_cmp = 0;
+static ir_type *lowered_type = NULL;
static ir_node *create_not(dbg_info *dbgi, ir_node *node)
{
return new_rd_Eor(dbgi, irg, block, node, one, lowered_mode);
}
-static int is_Const_0(const ir_node *node)
+static ir_node *create_convb(ir_node *node)
{
- tarval *tv;
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(node);
+ ir_node *conv = new_rd_Conv(NULL, irg, block, node, mode_b);
- if(!is_Const(node))
- return 0;
+ return conv;
+}
- tv = get_Const_tarval(node);
- return tarval_is_null(tv);
+static ir_type *create_lowered_type(void)
+{
+ if(lowered_type == NULL) {
+ lowered_type = new_type_primitive(new_id_from_str("__lowered_mode_b"),
+ lowered_mode);
+ }
+ return lowered_type;
+}
+
+static void adjust_method_type(ir_type *method_type)
+{
+ int i;
+ int n_params;
+ int n_res;
+
+ n_params = get_method_n_params(method_type);
+ for(i = 0; i < n_params; ++i) {
+ ir_type *param = get_method_param_type(method_type, i);
+ if(get_type_mode(param) == mode_b) {
+ set_method_param_type(method_type, i, create_lowered_type());
+ }
+ }
+
+ n_res = get_method_n_ress(method_type);
+ for(i = 0; i < n_res; ++i) {
+ ir_type *res_type = get_method_res_type(method_type, i);
+ if(get_type_mode(res_type) == mode_b) {
+ set_method_res_type(method_type, i, create_lowered_type());
+ }
+ }
}
static ir_node *lower_node(ir_node *node)
dbg_info *dbgi = get_irn_dbg_info(node);
ir_op *op = get_irn_op(node);
ir_node *block = get_nodes_block(node);
+ ir_node *res;
assert(get_irn_mode(node) == mode_b);
+ res = get_irn_link(node);
+ if(res != NULL)
+ return res;
+
/* TODO: be robust against phi-loops... */
+ if(op == op_Phi) {
+ int i, arity;
+ ir_node **in;
+ ir_node *unknown, *new_phi;
- if(op == op_And || op == op_Or || op == op_Eor || op == op_Phi) {
+ arity = get_irn_arity(node);
+ in = alloca(arity * sizeof(in[0]));
+ unknown = new_Unknown(lowered_mode);
+ for(i = 0; i < arity; ++i) {
+ in[i] = unknown;
+ }
+ new_phi = new_rd_Phi(dbgi, irg, block, arity, in,
+ lowered_mode);
+ set_irn_link(node, new_phi);
+
+ for(i = 0; i < arity; ++i) {
+ ir_node *in = get_irn_n(node, i);
+ ir_node *low_in = lower_node(in);
+
+ set_irn_n(new_phi, i, low_in);
+ }
+
+ return new_phi;
+ }
+
+ if(op == op_And || op == op_Or || op == op_Eor) {
int i, arity;
ir_node *copy = exact_copy(node);
set_irn_n(copy, i, low_in);
}
set_irn_mode(copy, lowered_mode);
+
+ set_irn_link(node, copy);
return copy;
}
if(op == op_Not) {
ir_node *op = get_Not_op(node);
ir_node *low_op = lower_node(op);
- return create_not(dbgi, low_op);
+ res = create_not(dbgi, low_op);
+ set_irn_link(node, res);
+ return res;
}
if(op == op_Psi) {
ir_node *cond = get_Psi_cond(node, 0);
ir_node *v_false = get_Psi_default(node);
ir_node *low_v_false = lower_node(v_false);
- ir_node *and0 = new_rd_And(dbgi, irg, block, cond, low_v_true,
+ ir_node *and0 = new_rd_And(dbgi, irg, block, low_cond, low_v_true,
lowered_mode);
ir_node *not_cond = create_not(dbgi, low_cond);
ir_node *or = new_rd_Or(dbgi, irg, block, and0, and1,
lowered_mode);
+ set_irn_link(node, or);
return or;
}
if(op == op_Conv) {
ir_node *one = new_d_Const(dbgi, lowered_mode, tv_one);
tarval *tv_zero = get_tarval_null(lowered_mode);
ir_node *zero = new_d_Const(dbgi, lowered_mode, tv_zero);
- ir_mode *mode = get_irn_mode(node);
+ ir_node *pred = get_Conv_op(node);
+ ir_mode *mode = get_irn_mode(pred);
tarval *tv_zeroc = get_tarval_null(mode);
ir_node *zero_cmp = new_d_Const(dbgi, mode, tv_zeroc);
- ir_node *cmp = new_rd_Cmp(dbgi, irg, block, node, zero_cmp);
+ ir_node *cmp = new_rd_Cmp(dbgi, irg, block, pred, zero_cmp);
ir_node *proj = new_rd_Proj(dbgi, irg, block, cmp, mode_b,
pn_Cmp_Lg);
ir_node *vals[2] = { one, zero };
ir_node *psi = new_rd_Psi(dbgi, irg, block, 1, &proj, vals,
lowered_mode);
+ set_irn_link(node, psi);
return psi;
}
if(op == op_Proj) {
- ir_node *pred = get_Proj_pred(node);
+ ir_node *pred = get_Proj_pred(node);
- assert(!is_Load(pred));
if(is_Cmp(pred)) {
- ir_mode *mode = get_irn_mode(pred);
ir_node *left = get_Cmp_left(pred);
ir_node *right = get_Cmp_right(pred);
+ ir_mode *mode = get_irn_mode(left);
- if(get_mode_size_bits(mode) < get_mode_size_bits(lowered_mode)
- || is_Const_0(right)) {
+ if ((mode_is_int(mode) || mode_is_reference(mode)) && (
+ get_mode_size_bits(mode) < get_mode_size_bits(lowered_mode) ||
+ (mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
+ )) {
int pnc = get_Proj_proj(node);
- ir_node *res = NULL;
int need_not = 0;
ir_node *a = NULL;
ir_node *b = NULL;
a = left;
b = right;
} else if(pnc == pn_Cmp_Le) {
- /* a <= b -> ((a - b) >> 31) ^ 1 */
+ /* a <= b -> ~(a - b) >> 31 */
a = right;
b = left;
need_not = 1;
a = right;
b = left;
} else if(pnc == pn_Cmp_Ge) {
- /* a >= b -> ((a - b) >> 31) ^ 1 */
+ /* a >= b -> ~(a - b) >> 31 */
a = left;
b = right;
need_not = 1;
}
if(a != NULL) {
- tarval *tv = new_tarval_from_long(31, mode_Iu);
+ tarval *tv = new_tarval_from_long(get_mode_size_bits(lowered_mode) - 1, mode_Iu);
ir_node *shift_cnt = new_d_Const(dbgi, mode_Iu, tv);
if(mode != lowered_mode) {
}
res = new_rd_Sub(dbgi, irg, block, a, b, lowered_mode);
+ if(need_not) {
+ res = new_rd_Not(dbgi, irg, block, res, lowered_mode);
+ }
res = new_rd_Shr(dbgi, irg, block, res, shift_cnt,
lowered_mode);
- if(need_not) {
- res = create_not(dbgi, res);
- }
+ set_irn_link(node, res);
return res;
}
}
ir_node *psi = new_rd_Psi(dbgi, irg, block, 1, &node, vals,
lowered_mode);
+ set_irn_link(node, psi);
return psi;
}
+ } else if(is_Proj(pred) && is_Call(get_Proj_pred(pred))) {
+ ir_type *type = get_Call_type(get_Proj_pred(pred));
+ adjust_method_type(type);
+ set_irn_mode(node, lowered_mode);
+ return node;
+ } else if(is_Proj(pred) && is_Start(get_Proj_pred(pred))) {
+ ir_entity *entity = get_irg_entity(irg);
+ ir_type *type = get_entity_type(entity);
+ adjust_method_type(type);
+ set_irn_mode(node, lowered_mode);
+ return node;
}
panic("unexpected projb: %+F (pred: %+F)", node, pred);
- set_irn_mode(node, lowered_mode);
- return node;
}
if(op == op_Const) {
tarval *tv = get_Const_tarval(node);
if(tv == get_tarval_b_true()) {
tarval *tv_one = get_tarval_one(lowered_mode);
- ir_node *one = new_d_Const(dbgi, lowered_mode, tv_one);
- return one;
+ res = new_d_Const(dbgi, lowered_mode, tv_one);
} else if(tv == get_tarval_b_false()) {
tarval *tv_zero = get_tarval_null(lowered_mode);
- ir_node *zero = new_d_Const(dbgi, lowered_mode, tv_zero);
- return zero;
+ res = new_d_Const(dbgi, lowered_mode, tv_zero);
} else {
panic("invalid boolean const %+F", node);
}
+ set_irn_link(node, res);
+ return res;
}
panic("didn't expect %+F to have mode_b", node);
}
-static ir_node *create_convb(ir_node *node)
-{
- ir_graph *irg = current_ir_graph;
- ir_node *block = get_nodes_block(node);
- ir_node *conv = new_rd_Conv(NULL, irg, block, node, mode_b);
-
- return conv;
-}
-
static void lower_mode_b_walker(ir_node *node, void *env)
{
int i, arity;
}
lowered_in = lower_node(in);
- lowered_in = create_convb(lowered_in);
+
+ if(is_Return(node)) {
+ ir_entity *entity = get_irg_entity(current_ir_graph);
+ ir_type *type = get_entity_type(entity);
+ adjust_method_type(type);
+ } else if(is_Call(node)) {
+ ir_type *type = get_Call_type(node);
+ adjust_method_type(type);
+ } else {
+ lowered_in = create_convb(lowered_in);
+ }
set_irn_n(node, i, lowered_in);
}
}
+static void clear_links(ir_node *node, void *env)
+{
+ (void) env;
+ set_irn_link(node, NULL);
+}
+
void ir_lower_mode_b(ir_graph *irg, ir_mode *mode, int do_lower_direct_cmp)
{
lowered_mode = mode;
lower_direct_cmp = do_lower_direct_cmp;
+ set_using_irn_link(irg);
+ irg_walk_graph(irg, clear_links, NULL, NULL);
irg_walk_graph(irg, lower_mode_b_walker, NULL, NULL);
+ clear_using_irn_link(irg);
}