X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Flower%2Flower_dw.c;h=0b9da801d10be9e8aafa2c643d10643191af2fd0;hb=dcff73b2cc34378750e2cc1979a4b120e19767a9;hp=cc75855781b340c3eb6710123c62ab3c9ab49289;hpb=85f517eca982e6a4e1d1848eb67634ae33b70de9;p=libfirm diff --git a/ir/lower/lower_dw.c b/ir/lower/lower_dw.c index cc7585578..0b9da801d 100644 --- a/ir/lower/lower_dw.c +++ b/ir/lower/lower_dw.c @@ -22,7 +22,6 @@ * @brief Lower double word operations, i.e. 64bit -> 32bit, 32bit -> 16bit etc. * @date 8.10.2004 * @author Michael Beck - * @version $Id$ */ #include "config.h" @@ -31,9 +30,11 @@ #include #include +#include "be.h" #include "error.h" #include "lowering.h" #include "irnode_t.h" +#include "irnodeset.h" #include "irgraph_t.h" #include "irmode_t.h" #include "iropt_t.h" @@ -46,6 +47,7 @@ #include "irgwalk.h" #include "ircons.h" #include "irflag.h" +#include "iroptimize.h" #include "irtools.h" #include "debug.h" #include "set.h" @@ -65,9 +67,15 @@ static set *conv_types; /** A map from a method type to its lowered type. */ static pmap *lowered_type; +/** A map from a builtin type to its lower and higher type. */ +static pmap *lowered_builtin_type_high; +static pmap *lowered_builtin_type_low; + /** The types for the binop and unop intrinsics. */ static ir_type *binop_tp_u, *binop_tp_s, *unop_tp_u, *unop_tp_s, *tp_s, *tp_u; +static ir_nodeset_t created_mux_nodes; + /** the debug handle */ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) @@ -102,7 +110,6 @@ typedef struct lower_dw_env_t { lower64_entry_t **entries; /**< entries per node */ ir_graph *irg; struct obstack obst; /**< an obstack holding the temporary data */ - ir_type *l_mtp; /**< lowered method type of the current method */ ir_tarval *tv_mode_bytes; /**< a tarval containing the number of bytes in the lowered modes */ ir_tarval *tv_mode_bits; /**< a tarval containing the number of bits in the lowered modes */ pdeq *waitq; /**< a wait queue of all nodes that must be handled later */ @@ -116,13 +123,11 @@ typedef struct lower_dw_env_t { const lwrdw_param_t *params; /**< transformation parameter */ unsigned flags; /**< some flags */ unsigned n_entries; /**< number of entries */ - ir_type *value_param_tp; /**< the old value param type */ } lower_dw_env_t; static lower_dw_env_t *env; static void lower_node(ir_node *node); -static bool mtp_must_be_lowered(ir_type *mtp); /** * Create a method type for a Conv emulation from imode to omode. @@ -136,7 +141,7 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode) key.omode = omode; key.mtd = NULL; - entry = (conv_tp_entry_t*)set_insert(conv_types, &key, sizeof(key), HASH_PTR(imode) ^ HASH_PTR(omode)); + entry = (conv_tp_entry_t*)set_insert(conv_types, &key, sizeof(key), hash_ptr(imode) ^ hash_ptr(omode)); if (! entry->mtd) { int n_param = 1, n_res = 1; @@ -151,8 +156,13 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode) /* set param types and result types */ n_param = 0; if (imode == env->high_signed) { - set_method_param_type(mtd, n_param++, tp_u); - set_method_param_type(mtd, n_param++, tp_s); + if (env->params->little_endian) { + set_method_param_type(mtd, n_param++, tp_u); + set_method_param_type(mtd, n_param++, tp_s); + } else { + set_method_param_type(mtd, n_param++, tp_s); + set_method_param_type(mtd, n_param++, tp_u); + } } else if (imode == env->high_unsigned) { set_method_param_type(mtd, n_param++, tp_u); set_method_param_type(mtd, n_param++, tp_u); @@ -163,8 +173,13 @@ static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode) n_res = 0; if (omode == env->high_signed) { - set_method_res_type(mtd, n_res++, tp_u); - set_method_res_type(mtd, n_res++, tp_s); + if (env->params->little_endian) { + set_method_res_type(mtd, n_res++, tp_u); + set_method_res_type(mtd, n_res++, tp_s); + } else { + set_method_res_type(mtd, n_res++, tp_s); + set_method_res_type(mtd, n_res++, tp_u); + } } else if (omode == env->high_unsigned) { set_method_res_type(mtd, n_res++, tp_u); set_method_res_type(mtd, n_res++, tp_u); @@ -287,6 +302,27 @@ static void prepare_links(ir_node *node) env->flags |= MUST_BE_LOWERED; } return; + } else if (is_Call(node)) { + /* Special case: If the result of the Call is never used, we won't + * find a Proj with a mode that potentially triggers MUST_BE_LOWERED + * to be set. Thus, if we see a call, we check its result types and + * decide whether MUST_BE_LOWERED has to be set. + */ + ir_type *tp = get_Call_type(node); + size_t n_res, i; + + n_res = get_method_n_ress(tp); + for (i = 0; i < n_res; ++i) { + ir_type *rtp = get_method_res_type(tp, i); + + if (is_Primitive_type(rtp)) { + ir_mode *rmode = get_type_mode(rtp); + + if (rmode == env->high_signed || rmode == env->high_unsigned) { + env->flags |= MUST_BE_LOWERED; + } + } + } } } @@ -478,7 +514,7 @@ static ir_node *get_intrinsic_address(ir_type *method, ir_op *op, key.ent = NULL; entry = (op_mode_entry_t*)set_insert(intrinsic_fkt, &key, sizeof(key), - HASH_PTR(op) ^ HASH_PTR(imode) ^ (HASH_PTR(omode) << 8)); + hash_ptr(op) ^ hash_ptr(imode) ^ (hash_ptr(omode) << 8)); if (! entry->ent) { /* create a new one */ ent = env->params->create_intrinsic(method, op, imode, omode, env->params->ctx); @@ -943,14 +979,14 @@ static void lower_Shl(ir_node *node, ir_mode *mode) * (and can't handle anything else) */ if (modulo_shift != get_mode_size_bits(shr_mode) || modulo_shift2<<1 != modulo_shift) { - panic("Shr lowering only implemented for modulo shift shr operations"); + panic("Shl lowering only implemented for modulo shift shl operations"); } if (!is_po2(modulo_shift) || !is_po2(modulo_shift2)) { - panic("Shr lowering only implemented for power-of-2 modes"); + panic("Shl lowering only implemented for power-of-2 modes"); } /* without 2-complement the -x instead of (bit_width-x) trick won't work */ if (get_mode_arithmetic(shr_mode) != irma_twos_complement) { - panic("Shr lowering only implemented for two-complement modes"); + panic("Shl lowering only implemented for two-complement modes"); } /* if the right operand is a 64bit value, we're only interested in the @@ -1171,6 +1207,23 @@ static void lower_Not(ir_node *node, ir_mode *mode) ir_set_dw_lowered(node, res_low, res_high); } +static void lower_Proj(ir_node *node, ir_mode *op_mode) +{ + ir_mode *mode = get_irn_mode(node); + ir_node *pred; + (void)op_mode; + if (mode != env->high_signed && mode != env->high_unsigned) + return; + /* skip tuples */ + pred = get_Proj_pred(node); + if (is_Tuple(pred)) { + long pn = get_Proj_proj(node); + ir_node *op = get_irn_n(pred, pn); + const lower64_entry_t *entry = get_node_entry(op); + ir_set_dw_lowered(node, entry->low_word, entry->high_word); + } +} + static bool is_equality_cmp(const ir_node *node) { ir_relation relation = get_Cmp_relation(node); @@ -1201,6 +1254,18 @@ static ir_node *get_cfop_destination(const ir_node *cfop) return get_edge_src_irn(first); } +static void lower_Switch(ir_node *node, ir_mode *high_mode) +{ + ir_node *selector = get_Switch_selector(node); + ir_mode *mode = get_irn_mode(selector); + (void)high_mode; + if (mode == env->high_signed || mode == env->high_unsigned) { + /* we can't really handle Switch with 64bit offsets */ + panic("Switch with 64bit jumptable not supported"); + } + lower_node(selector); +} + /** * Translate a Cond. */ @@ -1208,7 +1273,6 @@ static void lower_Cond(ir_node *node, ir_mode *high_mode) { ir_node *left, *right, *block; ir_node *sel = get_Cond_selector(node); - ir_mode *m = get_irn_mode(sel); ir_mode *cmp_mode; const lower64_entry_t *lentry, *rentry; ir_node *projT = NULL, *projF = NULL; @@ -1223,15 +1287,6 @@ static void lower_Cond(ir_node *node, ir_mode *high_mode) (void) high_mode; - if (m != mode_b) { - if (m == env->high_signed || m == env->high_unsigned) { - /* bad we can't really handle Switch with 64bit offsets */ - panic("Cond with 64bit jumptable not supported"); - } - lower_node(sel); - return; - } - if (!is_Cmp(sel)) { lower_node(sel); return; @@ -1286,7 +1341,7 @@ static void lower_Cond(ir_node *node, ir_mode *high_mode) ir_node *xor_low = new_rd_Eor(dbg, block, low_left, low_right, mode); ir_node *xor_high = new_rd_Eor(dbg, block, high_left, high_right, mode); ir_node *ornode = new_rd_Or(dbg, block, xor_low, xor_high, mode); - ir_node *cmp = new_rd_Cmp(dbg, block, ornode, new_r_Const_long(irg, mode, 0), relation); + ir_node *cmp = new_rd_Cmp(dbg, block, ornode, new_r_Const(irg, get_mode_null(mode)), relation); set_Cond_selector(node, cmp); return; } @@ -1464,8 +1519,13 @@ static void lower_Conv_to_Ll(ir_node *node) set_irn_pinned(call, get_irn_pinned(node)); irn = new_r_Proj(call, mode_T, pn_Call_T_result); - res_low = new_r_Proj(irn, low_unsigned, 0); - res_high = new_r_Proj(irn, low_signed, 1); + if (env->params->little_endian) { + res_low = new_r_Proj(irn, low_unsigned, 0); + res_high = new_r_Proj(irn, low_signed, 1); + } else { + res_low = new_r_Proj(irn, low_unsigned, 1); + res_high = new_r_Proj(irn, low_signed, 0); + } } ir_set_dw_lowered(node, res_low, res_high); } @@ -1503,8 +1563,13 @@ static void lower_Conv_from_Ll(ir_node *node) ir_node *res; irn = get_intrinsic_address(mtp, get_irn_op(node), imode, omode); - in[0] = entry->low_word; - in[1] = entry->high_word; + if (env->params->little_endian) { + in[0] = entry->low_word; + in[1] = entry->high_word; + } else { + in[0] = entry->high_word; + in[1] = entry->low_word; + } call = new_rd_Call(dbg, block, get_irg_no_mem(irg), irn, 2, in, mtp); set_irn_pinned(call, get_irn_pinned(node)); @@ -1551,7 +1616,7 @@ static void lower_Cmp(ir_node *cmp, ir_mode *m) ir_node *xor_low = new_rd_Eor(dbg, block, low_left, low_right, mode); ir_node *xor_high = new_rd_Eor(dbg, block, high_left, high_right, mode); ir_node *ornode = new_rd_Or(dbg, block, xor_low, xor_high, mode); - ir_node *new_cmp = new_rd_Cmp(dbg, block, ornode, new_r_Const_long(irg, mode, 0), relation); + ir_node *new_cmp = new_rd_Cmp(dbg, block, ornode, new_r_Const(irg, get_mode_null(mode)), relation); exchange(cmp, new_cmp); return; } @@ -1602,25 +1667,58 @@ static void lower_Conv(ir_node *node, ir_mode *mode) } } -/** - * Remember the new argument index of this value type entity in the lowered - * method type. - * - * @param ent the entity - * @param pos the argument index of this entity - */ -static inline void set_entity_arg_idx(ir_entity *ent, size_t pos) +static void fix_parameter_entities(ir_graph *irg, ir_type *orig_mtp) { - set_entity_link(ent, INT_TO_PTR(pos)); -} + size_t orig_n_params = get_method_n_params(orig_mtp); + ir_entity **parameter_entities; -/** - * Retrieve the argument index of a value type entity. - * - * @param ent the entity - */ -static size_t get_entity_arg_idx(const ir_entity *ent) { - return PTR_TO_INT(get_entity_link(ent)); + parameter_entities = ALLOCANZ(ir_entity*, orig_n_params); + + ir_type *frame_type = get_irg_frame_type(irg); + size_t n = get_compound_n_members(frame_type); + size_t i; + size_t n_param; + + /* collect parameter entities */ + for (i = 0; i < n; ++i) { + ir_entity *entity = get_compound_member(frame_type, i); + size_t p; + if (!is_parameter_entity(entity)) + continue; + p = get_entity_parameter_number(entity); + if (p == IR_VA_START_PARAMETER_NUMBER) + continue; + assert(p < orig_n_params); + assert(parameter_entities[p] == NULL); + parameter_entities[p] = entity; + } + + /* adjust indices */ + n_param = 0; + for (i = 0; i < orig_n_params; ++i, ++n_param) { + ir_entity *entity = parameter_entities[i]; + ir_type *tp; + + if (entity != NULL) + set_entity_parameter_number(entity, n_param); + + tp = get_method_param_type(orig_mtp, i); + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + if (mode == env->high_signed || mode == env->high_unsigned) { + ++n_param; + /* note that we do not change the type of the parameter + * entities, as calling convention fixup later still needs to + * know which is/was a lowered doubleword. + * So we just mark/remember it for later */ + if (entity != NULL) { + assert(entity->attr.parameter.doubleword_low_mode == NULL); + entity->attr.parameter.doubleword_low_mode + = env->low_unsigned; + } + } + } + } } /** @@ -1633,121 +1731,122 @@ static size_t get_entity_arg_idx(const ir_entity *ent) { */ static ir_type *lower_mtp(ir_type *mtp) { - pmap_entry *entry; - ir_type *res, *value_type; - - entry = pmap_find(lowered_type, mtp); - if (! entry) { - size_t i, orig_n_params, orig_n_res, n_param, n_res; + ir_type *res; + size_t i; + size_t orig_n_params; + size_t orig_n_res; + size_t n_param; + size_t n_res; + bool must_be_lowered; + + res = (ir_type*)pmap_get(lowered_type, mtp); + if (res != NULL) + return res; + if (type_visited(mtp)) + return mtp; + mark_type_visited(mtp); + + orig_n_params = get_method_n_params(mtp); + orig_n_res = get_method_n_ress(mtp); + n_param = orig_n_params; + n_res = orig_n_res; + must_be_lowered = false; + + /* count new number of params */ + for (i = orig_n_params; i > 0;) { + ir_type *tp = get_method_param_type(mtp, --i); - /* count new number of params */ - n_param = orig_n_params = get_method_n_params(mtp); - for (i = orig_n_params; i > 0;) { - ir_type *tp = get_method_param_type(mtp, --i); - - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); - if (mode == env->high_signed || mode == env->high_unsigned) - ++n_param; + if (mode == env->high_signed || mode == env->high_unsigned) { + ++n_param; + must_be_lowered = true; } } + } - /* count new number of results */ - n_res = orig_n_res = get_method_n_ress(mtp); - for (i = orig_n_res; i > 0;) { - ir_type *tp = get_method_res_type(mtp, --i); + /* count new number of results */ + for (i = orig_n_res; i > 0;) { + ir_type *tp = get_method_res_type(mtp, --i); - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); - if (mode == env->high_signed || mode == env->high_unsigned) - ++n_res; + if (mode == env->high_signed || mode == env->high_unsigned) { + ++n_res; + must_be_lowered = true; } } + } + if (!must_be_lowered) { + set_type_link(mtp, NULL); + return mtp; + } - res = new_type_method(n_param, n_res); + res = new_d_type_method(n_param, n_res, get_type_dbg_info(mtp)); - /* set param types and result types */ - for (i = n_param = 0; i < orig_n_params; ++i) { - ir_type *tp = get_method_param_type(mtp, i); - - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); - - if (mode == env->high_signed) { - if (env->params->little_endian) { - set_method_param_type(res, n_param++, tp_u); - set_method_param_type(res, n_param++, tp_s); - } else { - set_method_param_type(res, n_param++, tp_s); - set_method_param_type(res, n_param++, tp_u); - } - } else if (mode == env->high_unsigned) { - set_method_param_type(res, n_param++, tp_u); + /* set param types and result types */ + for (i = n_param = 0; i < orig_n_params; ++i) { + ir_type *tp = get_method_param_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { set_method_param_type(res, n_param++, tp_u); + set_method_param_type(res, n_param++, tp_s); } else { - set_method_param_type(res, n_param++, tp); + set_method_param_type(res, n_param++, tp_s); + set_method_param_type(res, n_param++, tp_u); } + } else if (mode == env->high_unsigned) { + set_method_param_type(res, n_param++, tp_u); + set_method_param_type(res, n_param++, tp_u); } else { - set_method_param_type(res, n_param++, tp); + set_method_param_type(res, n_param, tp); + ++n_param; } + } else { + set_method_param_type(res, n_param, tp); + ++n_param; } - for (i = n_res = 0; i < orig_n_res; ++i) { - ir_type *tp = get_method_res_type(mtp, i); - - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); - - if (mode == env->high_signed) { - if (env->params->little_endian) { - set_method_res_type(res, n_res++, tp_u); - set_method_res_type(res, n_res++, tp_s); - } else { - set_method_res_type(res, n_res++, tp_s); - set_method_res_type(res, n_res++, tp_u); - } - } else if (mode == env->high_unsigned) { - set_method_res_type(res, n_res++, tp_u); + } + for (i = n_res = 0; i < orig_n_res; ++i) { + ir_type *tp = get_method_res_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { set_method_res_type(res, n_res++, tp_u); + set_method_res_type(res, n_res++, tp_s); } else { - set_method_res_type(res, n_res++, tp); + set_method_res_type(res, n_res++, tp_s); + set_method_res_type(res, n_res++, tp_u); } + } else if (mode == env->high_unsigned) { + set_method_res_type(res, n_res++, tp_u); + set_method_res_type(res, n_res++, tp_u); } else { set_method_res_type(res, n_res++, tp); } + } else { + set_method_res_type(res, n_res++, tp); } - set_lowered_type(mtp, res); - pmap_insert(lowered_type, mtp, res); - - value_type = get_method_value_param_type(mtp); - if (value_type != NULL) { - /* this creates a new value parameter type */ - (void)get_method_value_param_ent(res, 0); - - /* set new param positions for all entities of the value type */ - for (i = n_param = 0; i < orig_n_params; ++i) { - ir_type *tp = get_method_param_type(mtp, i); - ir_entity *ent = get_method_value_param_ent(mtp, i); - - set_entity_arg_idx(ent, n_param); - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); - - if (mode == env->high_signed - || mode == env->high_unsigned) { - n_param += 2; - continue; - } - } - ++n_param; - } - - set_lowered_type(value_type, get_method_value_param_type(res)); - } - } else { - res = (ir_type*)entry->value; } + + set_method_variadicity(res, get_method_variadicity(mtp)); + set_method_calling_convention(res, get_method_calling_convention(mtp)); + set_method_additional_properties(res, get_method_additional_properties(mtp)); + + set_higher_type(res, mtp); + set_type_link(res, mtp); + + mark_type_visited(res); + pmap_insert(lowered_type, mtp, res); return res; } @@ -1778,32 +1877,31 @@ static void lower_Return(ir_node *node, ir_mode *mode) ent = get_irg_entity(irg); mtp = get_entity_type(ent); - mtp = lower_mtp(mtp); - set_entity_type(ent, mtp); - /* create a new in array */ NEW_ARR_A(ir_node *, in, get_method_n_ress(mtp) + 1); - in[0] = get_Return_mem(node); + j = 0; + in[j++] = get_Return_mem(node); - for (j = i = 0, n = get_Return_n_ress(node); i < n; ++i) { + for (i = 0, n = get_Return_n_ress(node); i < n; ++i) { ir_node *pred = get_Return_res(node, i); ir_mode *pred_mode = get_irn_mode(pred); if (pred_mode == env->high_signed || pred_mode == env->high_unsigned) { const lower64_entry_t *entry = get_node_entry(pred); if (env->params->little_endian) { - in[++j] = entry->low_word; - in[++j] = entry->high_word; + in[j++] = entry->low_word; + in[j++] = entry->high_word; } else { - in[++j] = entry->high_word; - in[++j] = entry->low_word; + in[j++] = entry->high_word; + in[j++] = entry->low_word; } } else { - in[++j] = pred; + in[j++] = pred; } } + assert(j == get_method_n_ress(mtp)+1); - set_irn_in(node, j+1, in); + set_irn_in(node, j, in); } /** @@ -1811,9 +1909,10 @@ static void lower_Return(ir_node *node, ir_mode *mode) */ static void lower_Start(ir_node *node, ir_mode *high_mode) { - ir_graph *irg = get_irn_irg(node); - ir_entity *ent = get_irg_entity(irg); - ir_type *tp = get_entity_type(ent); + ir_graph *irg = get_irn_irg(node); + ir_entity *ent = get_irg_entity(irg); + ir_type *mtp = get_entity_type(ent); + ir_type *orig_mtp = get_type_link(mtp); ir_node *args; long *new_projs; size_t i, j, n_params; @@ -1821,16 +1920,18 @@ static void lower_Start(ir_node *node, ir_mode *high_mode) const ir_edge_t *next; (void) high_mode; - if (!mtp_must_be_lowered(tp)) + /* if type link is NULL then the type was not lowered, hence no changes + * at Start necessary */ + if (orig_mtp == NULL) return; - n_params = get_method_n_params(tp); + n_params = get_method_n_params(orig_mtp); NEW_ARR_A(long, new_projs, n_params); /* Calculate mapping of proj numbers in new_projs */ for (i = j = 0; i < n_params; ++i, ++j) { - ir_type *ptp = get_method_param_type(tp, i); + ir_type *ptp = get_method_param_type(orig_mtp, i); new_projs[i] = j; if (is_Primitive_type(ptp)) { @@ -1840,10 +1941,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode) } } - /* lower method type */ - tp = lower_mtp(tp); - set_entity_type(ent, tp); - + /* find args Proj */ args = NULL; foreach_out_edge(node, edge) { ir_node *proj = get_edge_src_irn(edge); @@ -1867,6 +1965,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode) ir_mode *mode_h; ir_node *res_low; ir_node *res_high; + int old_cse; dbg_info *dbg; if (!is_Proj(proj)) @@ -1884,6 +1983,9 @@ static void lower_Start(ir_node *node, ir_mode *high_mode) continue; } + /* Switch off CSE or we might get an already existing Proj. */ + old_cse = get_opt_cse(); + set_opt_cse(0); dbg = get_irn_dbg_info(proj); if (env->params->little_endian) { res_low = new_rd_Proj(dbg, pred, mode_l, new_projs[proj_nr]); @@ -1892,6 +1994,7 @@ static void lower_Start(ir_node *node, ir_mode *high_mode) res_high = new_rd_Proj(dbg, pred, mode_h, new_projs[proj_nr]); res_low = new_rd_Proj(dbg, pred, mode_l, new_projs[proj_nr] + 1); } + set_opt_cse(old_cse); ir_set_dw_lowered(proj, res_low, res_high); } } @@ -2260,23 +2363,410 @@ static void lower_ASM(ir_node *asmn, ir_mode *mode) } /** - * Translate a Sel node. + * Lower the builtin type to its higher part. + * + * @param mtp the builtin type to lower + * + * @return the lowered type + */ +static ir_type *lower_Builtin_type_high(ir_type *mtp) +{ + ir_type *res; + size_t i; + size_t n_params; + size_t n_results; + bool must_be_lowered; + + res = (ir_type*)pmap_get(lowered_builtin_type_high, mtp); + if (res != NULL) + return res; + + n_params = get_method_n_params(mtp); + n_results = get_method_n_ress(mtp); + must_be_lowered = false; + + /* check for double word parameter */ + for (i = n_params; i > 0;) { + ir_type *tp = get_method_param_type(mtp, --i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed || mode == env->high_unsigned) { + must_be_lowered = true; + break; + } + } + } + + if (!must_be_lowered) { + set_type_link(mtp, NULL); + return mtp; + } + + res = new_d_type_method(n_params, n_results, get_type_dbg_info(mtp)); + + /* set param types and result types */ + for (i = 0; i < n_params; ++i) { + ir_type *tp = get_method_param_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { + set_method_param_type(res, i, tp_u); + } else { + set_method_param_type(res, i, tp_s); + } + } else if (mode == env->high_unsigned) { + set_method_param_type(res, i, tp_u); + } else { + set_method_param_type(res, i, tp); + } + } else { + set_method_param_type(res, i, tp); + } + } + for (i = n_results = 0; i < n_results; ++i) { + ir_type *tp = get_method_res_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { + set_method_res_type(res, i, tp_u); + } else { + set_method_res_type(res, i, tp_s); + } + } else if (mode == env->high_unsigned) { + set_method_res_type(res, i, tp_u); + } else { + set_method_res_type(res, i, tp); + } + } else { + set_method_res_type(res, i, tp); + } + } + + set_method_variadicity(res, get_method_variadicity(mtp)); + set_method_calling_convention(res, get_method_calling_convention(mtp)); + set_method_additional_properties(res, get_method_additional_properties(mtp)); + + pmap_insert(lowered_builtin_type_high, mtp, res); + return res; +} + +/** + * Lower the builtin type to its lower part. + * + * @param mtp the builtin type to lower + * + * @return the lowered type + */ +static ir_type *lower_Builtin_type_low(ir_type *mtp) +{ + ir_type *res; + size_t i; + size_t n_params; + size_t n_results; + bool must_be_lowered; + + res = (ir_type*)pmap_get(lowered_builtin_type_low, mtp); + if (res != NULL) + return res; + + n_params = get_method_n_params(mtp); + n_results = get_method_n_ress(mtp); + must_be_lowered = false; + + /* check for double word parameter */ + for (i = n_params; i > 0;) { + ir_type *tp = get_method_param_type(mtp, --i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed || mode == env->high_unsigned) { + must_be_lowered = true; + break; + } + } + } + + if (!must_be_lowered) { + set_type_link(mtp, NULL); + return mtp; + } + + res = new_d_type_method(n_params, n_results, get_type_dbg_info(mtp)); + + /* set param types and result types */ + for (i = 0; i < n_params; ++i) { + ir_type *tp = get_method_param_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { + set_method_param_type(res, i, tp_s); + } else { + set_method_param_type(res, i, tp_u); + } + } else if (mode == env->high_unsigned) { + set_method_param_type(res, i, tp_u); + } else { + set_method_param_type(res, i, tp); + } + } else { + set_method_param_type(res, i, tp); + } + } + for (i = 0; i < n_results; ++i) { + ir_type *tp = get_method_res_type(mtp, i); + + if (is_Primitive_type(tp)) { + ir_mode *mode = get_type_mode(tp); + + if (mode == env->high_signed) { + if (env->params->little_endian) { + set_method_res_type(res, i, tp_s); + } else { + set_method_res_type(res, i, tp_u); + } + } else if (mode == env->high_unsigned) { + set_method_res_type(res, i, tp_u); + } else { + set_method_res_type(res, i, tp); + } + } else { + set_method_res_type(res, i, tp); + } + } + + set_method_variadicity(res, get_method_variadicity(mtp)); + set_method_calling_convention(res, get_method_calling_convention(mtp)); + set_method_additional_properties(res, get_method_additional_properties(mtp)); + + pmap_insert(lowered_builtin_type_low, mtp, res); + return res; +} + +/** + * lowers a builtin which reduces a 64bit value to a simple summary value + * (popcount, ffs, ...) + */ +static void lower_reduce_builtin(ir_node *builtin, ir_mode *mode) +{ + ir_builtin_kind kind = get_Builtin_kind(builtin); + ir_node *operand = get_Builtin_param(builtin, 0); + ir_mode *operand_mode = get_irn_mode(operand); + if (operand_mode != env->high_signed && operand_mode != env->high_unsigned) + return; + + { + arch_allow_ifconv_func allow_ifconv = be_get_backend_param()->allow_ifconv; + int arity = get_irn_arity(builtin); + dbg_info *dbgi = get_irn_dbg_info(builtin); + ir_graph *irg = get_irn_irg(builtin); + ir_type *type = get_Builtin_type(builtin); + ir_type *lowered_type_high = lower_Builtin_type_high(type); + ir_type *lowered_type_low = lower_Builtin_type_low(type); + ir_type *result_type = get_method_res_type(lowered_type_low, 0); + ir_mode *result_mode = get_type_mode(result_type); + ir_node *block = get_nodes_block(builtin); + ir_node *mem = get_Builtin_mem(builtin); + const lower64_entry_t *entry = get_node_entry(operand); + ir_mode *high_mode = get_irn_mode(entry->high_word); + ir_node *in_high[1] = {entry->high_word}; + ir_node *in_low[1] = {entry->low_word}; + ir_node *res; + + assert(is_NoMem(mem)); + assert(arity == 2); + + switch (kind) { + case ir_bk_ffs: { + ir_node *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(env->low_unsigned)); + ir_node *zero_high = new_rd_Const(dbgi, irg, get_mode_null(high_mode)); + ir_node *zero_unsigned = new_rd_Const(dbgi, irg, get_mode_null(env->low_unsigned)); + ir_node *zero_result = new_rd_Const(dbgi, irg, get_mode_null(result_mode)); + ir_node *cmp_low = new_rd_Cmp(dbgi, block, entry->low_word, zero_unsigned, ir_relation_equal); + ir_node *cmp_high = new_rd_Cmp(dbgi, block, entry->high_word, zero_high, ir_relation_equal); + ir_node *ffs_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + ir_node *high_proj = new_r_Proj(ffs_high, result_mode, pn_Builtin_max+1); + ir_node *high = new_rd_Add(dbgi, block, high_proj, number_of_bits, result_mode); + ir_node *ffs_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + ir_node *low = new_r_Proj(ffs_low, result_mode, pn_Builtin_max+1); + ir_node *mux_high = new_rd_Mux(dbgi, block, cmp_high, high, zero_result, result_mode); + + if (! allow_ifconv(cmp_high, high, zero_result)) + ir_nodeset_insert(&created_mux_nodes, mux_high); + + res = new_rd_Mux(dbgi, block, cmp_low, low, mux_high, result_mode); + + if (! allow_ifconv(cmp_low, low, mux_high)) + ir_nodeset_insert(&created_mux_nodes, res); + break; + } + case ir_bk_clz: { + ir_node *zero = new_rd_Const(dbgi, irg, get_mode_null(high_mode)); + ir_node *cmp_high = new_rd_Cmp(dbgi, block, entry->high_word, zero, ir_relation_equal); + ir_node *clz_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + ir_node *high = new_r_Proj(clz_high, result_mode, pn_Builtin_max+1); + ir_node *clz_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + ir_node *low_proj = new_r_Proj(clz_low, result_mode, pn_Builtin_max+1); + ir_node *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(mode)); + ir_node *low = new_rd_Add(dbgi, block, low_proj, number_of_bits, result_mode); + + res = new_rd_Mux(dbgi, block, cmp_high, high, low, result_mode); + + if (! allow_ifconv(cmp_high, high, low)) + ir_nodeset_insert(&created_mux_nodes, res); + break; + } + case ir_bk_ctz: { + ir_node *zero_unsigned = new_rd_Const(dbgi, irg, get_mode_null(env->low_unsigned)); + ir_node *cmp_low = new_rd_Cmp(dbgi, block, entry->low_word, zero_unsigned, ir_relation_equal); + ir_node *ffs_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + ir_node *high_proj = new_r_Proj(ffs_high, result_mode, pn_Builtin_max+1); + ir_node *number_of_bits = new_r_Const_long(irg, result_mode, get_mode_size_bits(env->low_unsigned)); + ir_node *high = new_rd_Add(dbgi, block, high_proj, number_of_bits, result_mode); + ir_node *ffs_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + ir_node *low = new_r_Proj(ffs_low, result_mode, pn_Builtin_max+1); + + res = new_rd_Mux(dbgi, block, cmp_low, low, high, result_mode); + + if (! allow_ifconv(cmp_low, low, high)) + ir_nodeset_insert(&created_mux_nodes, res); + break; + } + case ir_bk_popcount: { + ir_node *popcount_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + ir_node *popcount_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + ir_node *high = new_r_Proj(popcount_high, result_mode, pn_Builtin_max+1); + ir_node *low = new_r_Proj(popcount_low, result_mode, pn_Builtin_max+1); + + res = new_rd_Add(dbgi, block, high, low, result_mode); + break; + } + case ir_bk_parity: { + ir_node *parity_high; + ir_node *parity_low; + ir_node *high; + ir_node *low; + + assert(arity == 2); + + parity_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + high = new_r_Proj(parity_high, result_mode, pn_Builtin_max+1); + parity_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + low = new_r_Proj(parity_low, result_mode, pn_Builtin_max+1); + res = new_rd_Eor(dbgi, block, high, low, result_mode); + break; + } + default: + panic("unexpected builtin"); + } + + turn_into_tuple(builtin, 2); + set_irn_n(builtin, pn_Builtin_M, mem); + set_irn_n(builtin, pn_Builtin_max+1, res); + } +} + +/** + * lowers builtins performing arithmetic (bswap) */ -static void lower_Sel(ir_node *sel, ir_mode *mode) +static void lower_arithmetic_builtin(ir_node *builtin, ir_mode *mode) { + ir_builtin_kind kind = get_Builtin_kind(builtin); + ir_node *operand = get_Builtin_param(builtin, 0); + ir_mode *operand_mode = get_irn_mode(operand); (void) mode; + if (operand_mode != env->high_signed && operand_mode != env->high_unsigned) + return; - /* we must only lower value parameter Sels if we change the - value parameter type. */ - if (env->value_param_tp != NULL) { - ir_entity *ent = get_Sel_entity(sel); - if (get_entity_owner(ent) == env->value_param_tp) { - size_t pos = get_entity_arg_idx(ent); + { + dbg_info *dbgi = get_irn_dbg_info(builtin); + ir_type *type = get_Builtin_type(builtin); + ir_type *lowered_type_high = lower_Builtin_type_high(type); + ir_type *lowered_type_low = lower_Builtin_type_low(type); + ir_node *block = get_nodes_block(builtin); + ir_node *mem = get_Builtin_mem(builtin); + const lower64_entry_t *entry = get_node_entry(operand); + ir_mode *mode_high = get_irn_mode(entry->high_word); + const ir_edge_t *edge; + const ir_edge_t *next; + ir_node *res_high; + ir_node *res_low; + + switch (kind) { + case ir_bk_bswap: { + ir_node *in_high[1] = { entry->high_word }; + ir_node *in_low[1] = { entry->low_word }; + ir_node *swap_high = new_rd_Builtin(dbgi, block, mem, 1, in_high, kind, lowered_type_high); + ir_node *swap_low = new_rd_Builtin(dbgi, block, mem, 1, in_low, kind, lowered_type_low); + ir_node *high = new_r_Proj(swap_high, mode_high, pn_Builtin_max+1); + ir_node *low = new_r_Proj(swap_low, env->low_unsigned, pn_Builtin_max+1); + if (mode_high == env->low_signed) { + res_high = new_rd_Conv(dbgi, block, low, env->low_signed); + res_low = new_rd_Conv(dbgi, block, high, env->low_unsigned); + } else { + res_high = low; + res_low = high; + } + break; + } + default: + panic("unexpected builtin"); + } + + /* search result Proj */ + foreach_out_edge_safe(builtin, edge, next) { + ir_node *proj = get_edge_src_irn(edge); + if (!is_Proj(proj)) + continue; - ent = get_method_value_param_ent(env->l_mtp, pos); - set_Sel_entity(sel, ent); + if (get_Proj_proj(proj) == pn_Builtin_max+1) { + ir_set_dw_lowered(proj, res_low, res_high); } } + } +} + +/** + * Lower double word builtins. + */ +static void lower_Builtin(ir_node *builtin, ir_mode *mode) +{ + ir_builtin_kind kind = get_Builtin_kind(builtin); + + switch (kind) { + case ir_bk_trap: + case ir_bk_debugbreak: + case ir_bk_return_address: + case ir_bk_frame_address: + case ir_bk_prefetch: + case ir_bk_inport: + case ir_bk_outport: + case ir_bk_inner_trampoline: + /* Nothing to do. */ + return; + case ir_bk_bswap: + lower_arithmetic_builtin(builtin, mode); + return; + case ir_bk_ffs: + case ir_bk_clz: + case ir_bk_ctz: + case ir_bk_popcount: + case ir_bk_parity: + lower_reduce_builtin(builtin, mode); + return; + } + panic("unknown builtin"); } /** @@ -2286,6 +2776,7 @@ static bool always_lower(unsigned code) { switch (code) { case iro_ASM: + case iro_Builtin: case iro_Proj: case iro_Start: case iro_Call: @@ -2331,43 +2822,20 @@ void ir_register_dw_lower_function(ir_op *op, lower_dw_func func) op->ops.generic = (op_func)func; } -/** - * Returns non-zero if a method type must be lowered. - * - * @param mtp the method type - */ -static bool mtp_must_be_lowered(ir_type *mtp) -{ - size_t i, n_params = get_method_n_params(mtp); - - /* first check if we have parameters that must be fixed */ - for (i = 0; i < n_params; ++i) { - ir_type *tp = get_method_param_type(mtp, i); - - if (is_Primitive_type(tp)) { - ir_mode *mode = get_type_mode(tp); - - if (mode == env->high_signed || mode == env->high_unsigned) - return true; - } - } - return false; -} - /* Determine which modes need to be lowered */ static void setup_modes(void) { unsigned size_bits = env->params->doubleword_size; ir_mode *doubleword_signed = NULL; ir_mode *doubleword_unsigned = NULL; - size_t n_modes = get_irp_n_modes(); + size_t n_modes = ir_get_n_modes(); ir_mode_arithmetic arithmetic; unsigned modulo_shift; size_t i; /* search for doubleword modes... */ for (i = 0; i < n_modes; ++i) { - ir_mode *mode = get_irp_mode(i); + ir_mode *mode = ir_get_mode(i); if (!mode_is_int(mode)) continue; if (get_mode_size_bits(mode) != size_bits) @@ -2421,10 +2889,10 @@ static void setup_modes(void) /* produce lowered modes */ env->high_signed = doubleword_signed; env->high_unsigned = doubleword_unsigned; - env->low_signed = new_ir_mode("WS", irms_int_number, size_bits, 1, - arithmetic, modulo_shift); - env->low_unsigned = new_ir_mode("WU", irms_int_number, size_bits, 0, - arithmetic, modulo_shift); + env->low_signed = new_int_mode("WS", arithmetic, size_bits, 1, + modulo_shift); + env->low_unsigned = new_int_mode("WU", arithmetic, size_bits, 0, + modulo_shift); } static void enqueue_preds(ir_node *node) @@ -2512,6 +2980,7 @@ static void lower_irg(ir_graph *irg) { ir_entity *ent; ir_type *mtp; + ir_type *lowered_mtp; unsigned n_idx; obstack_init(&env->obst); @@ -2527,19 +2996,17 @@ static void lower_irg(ir_graph *irg) memset(env->entries, 0, sizeof(env->entries[0]) * n_idx); env->irg = irg; - env->l_mtp = NULL; env->flags = 0; - env->value_param_tp = NULL; ent = get_irg_entity(irg); mtp = get_entity_type(ent); + lowered_mtp = lower_mtp(mtp); - if (mtp_must_be_lowered(mtp)) { - ir_type *ltp = lower_mtp(mtp); - /* Do not update the entity type yet, this will be done by lower_Start! */ + if (lowered_mtp != mtp) { + set_entity_type(ent, lowered_mtp); env->flags |= MUST_BE_LOWERED; - env->l_mtp = ltp; - env->value_param_tp = get_method_value_param_type(mtp); + + fix_parameter_entities(irg, mtp); } /* first step: link all nodes and allocate data */ @@ -2573,8 +3040,8 @@ static void lower_irg(ir_graph *irg) if (env->flags & CF_CHANGED) { /* control flow changed, dominance info is invalid */ - set_irg_doms_inconsistent(irg); - set_irg_extblk_inconsistent(irg); + clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE + | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS); } edges_deactivate(irg); } @@ -2594,11 +3061,12 @@ void ir_prepare_dw_lowering(const lwrdw_param_t *new_param) param = new_param; - clear_irp_opcodes_generic_func(); + ir_clear_opcodes_generic_func(); ir_register_dw_lower_function(op_ASM, lower_ASM); ir_register_dw_lower_function(op_Add, lower_binop); ir_register_dw_lower_function(op_And, lower_And); ir_register_dw_lower_function(op_Bad, lower_Bad); + ir_register_dw_lower_function(op_Builtin, lower_Builtin); ir_register_dw_lower_function(op_Call, lower_Call); ir_register_dw_lower_function(op_Cmp, lower_Cmp); ir_register_dw_lower_function(op_Cond, lower_Cond); @@ -2613,17 +3081,26 @@ void ir_prepare_dw_lowering(const lwrdw_param_t *new_param) ir_register_dw_lower_function(op_Mux, lower_Mux); ir_register_dw_lower_function(op_Not, lower_Not); ir_register_dw_lower_function(op_Or, lower_Or); + ir_register_dw_lower_function(op_Proj, lower_Proj); ir_register_dw_lower_function(op_Return, lower_Return); - ir_register_dw_lower_function(op_Sel, lower_Sel); ir_register_dw_lower_function(op_Shl, lower_Shl); ir_register_dw_lower_function(op_Shr, lower_Shr); ir_register_dw_lower_function(op_Shrs, lower_Shrs); ir_register_dw_lower_function(op_Start, lower_Start); ir_register_dw_lower_function(op_Store, lower_Store); ir_register_dw_lower_function(op_Sub, lower_binop); + ir_register_dw_lower_function(op_Switch, lower_Switch); ir_register_dw_lower_function(op_Unknown, lower_Unknown); } +/** + * Callback to lower only the Mux nodes we created. + */ +static int lower_mux_cb(ir_node *mux) +{ + return ir_nodeset_contains(&created_mux_nodes, mux); +} + /* * Do the lowering. */ @@ -2645,6 +3122,10 @@ void ir_lower_dw_ops(void) conv_types = new_set(cmp_conv_tp, 16); if (! lowered_type) lowered_type = pmap_create(); + if (! lowered_builtin_type_low) + lowered_builtin_type_low = pmap_create(); + if (! lowered_builtin_type_high) + lowered_builtin_type_high = pmap_create(); /* create a primitive unsigned and signed type */ if (! tp_u) @@ -2708,11 +3189,22 @@ void ir_lower_dw_ops(void) lenv.first_id = new_id_from_chars(param->little_endian ? ".l" : ".h", 2); lenv.next_id = new_id_from_chars(param->little_endian ? ".h" : ".l", 2); + irp_reserve_resources(irp, IRP_RESOURCE_TYPE_LINK | IRP_RESOURCE_TYPE_VISITED); + inc_master_type_visited(); /* transform all graphs */ for (i = 0, n = get_irp_n_irgs(); i < n; ++i) { ir_graph *irg = get_irp_irg(i); + + ir_nodeset_init(&created_mux_nodes); + lower_irg(irg); + + if (ir_nodeset_size(&created_mux_nodes) > 0) + lower_mux(irg, lower_mux_cb); + + ir_nodeset_destroy(&created_mux_nodes); } + irp_free_resources(irp, IRP_RESOURCE_TYPE_LINK | IRP_RESOURCE_TYPE_VISITED); del_pdeq(lenv.waitq); env = NULL;