From: Michael Beck Date: Sat, 3 Jan 2009 04:48:20 +0000 (+0000) Subject: - add intrinsic lowerer for strcpy(), mempcpy(), memmove(), strncmp() X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;ds=sidebyside;h=e80283923187f85a74c3e6aa7d50b29b7f0e79cf;p=libfirm - add intrinsic lowerer for strcpy(), mempcpy(), memmove(), strncmp() [r25072] --- diff --git a/include/libfirm/firmstat.h b/include/libfirm/firmstat.h index 872079fa2..9151f7496 100644 --- a/include/libfirm/firmstat.h +++ b/include/libfirm/firmstat.h @@ -140,9 +140,13 @@ enum firmstat_optimizations_t { FS_OPT_RTS_SYMMETRIC, /**< RTS optimization: call to symmetric function f(-x) replaced by f(x) */ FS_OPT_RTS_STRCMP, /**< RTS optimization: call to strcmp() replaced */ FS_OPT_RTS_STRNCMP, /**< RTS optimization: call to strncmp() replaced */ + FS_OPT_RTS_STRCPY, /**< RTS optimization: call to strcpy() replaced */ + FS_OPT_RTS_STRLEN, /**< RTS optimization: call to strlen() replaced */ FS_OPT_RTS_MEMCPY, /**< RTS optimization: call to memcpy() replaced */ + FS_OPT_RTS_MEMPCPY, /**< RTS optimization: call to mempcpy() replaced */ + FS_OPT_RTS_MEMMOVE, /**< RTS optimization: call to memmove() replaced */ FS_OPT_RTS_MEMSET, /**< RTS optimization: call to memset() replaced */ - FS_OPT_RTS_STRLEN, /**< RTS optimization: call to strlen() replaced */ + FS_OPT_RTS_MEMCMP, /**< RTS optimization: call to memcmp() replaced */ FS_BE_IA32_LEA, /**< Lea was created */ FS_BE_IA32_LOAD_LEA, /**< Load merged with a Lea */ FS_BE_IA32_STORE_LEA, /**< Store merged with a Lea */ diff --git a/include/libfirm/lowering.h b/include/libfirm/lowering.h index 3c3111f4b..4d609793e 100644 --- a/include/libfirm/lowering.h +++ b/include/libfirm/lowering.h @@ -436,6 +436,13 @@ int i_mapper_strcmp(ir_node *call, void *ctx); */ int i_mapper_strncmp(ir_node *call, void *ctx); +/** + * A mapper for the strcpy-Function: char pointer strcpy(char pointer a, char pointer b); + * + * @return 0 if the strcpy call was removed, 0 else. + */ +int i_mapper_strcpy(ir_node *call, void *ctx); + /** * A mapper for the strlen-Function: inttype strlen(char pointer a); * @@ -450,6 +457,20 @@ int i_mapper_strlen(ir_node *call, void *ctx); */ int i_mapper_memcpy(ir_node *call, void *ctx); +/** + * A mapper for the mempcpy-Function: void pointer mempcpy(void pointer d, void pointer s, inttype c); + * + * @return 0 if the mempcpy call was removed, 0 else. + */ +int i_mapper_mempcpy(ir_node *call, void *ctx); + +/** + * A mapper for the memmove-Function: void pointer memmove(void pointer d, void pointer s, inttype c); + * + * @return 0 if the memmove call was removed, 0 else. + */ +int i_mapper_memmove(ir_node *call, void *ctx); + /** * A mapper for the memset-Function: void pointer memset(void pointer d, inttype C, inttype len); * @@ -457,6 +478,13 @@ int i_mapper_memcpy(ir_node *call, void *ctx); */ int i_mapper_memset(ir_node *call, void *ctx); +/** + * A mapper for the strncmp-Function: inttype memcmp(void pointer a, void pointer b, inttype len); + * + * @return 0 if the strncmp call was removed, 0 else. + */ +int i_mapper_memcmp(ir_node *call, void *ctx); + /** * A mapper for the alloca() function: pointer alloca(inttype size) * Replaces the call by a Alloca(stack_alloc) node. diff --git a/ir/lower/lower_intrinsics.c b/ir/lower/lower_intrinsics.c index 504881387..904b1337c 100644 --- a/ir/lower/lower_intrinsics.c +++ b/ir/lower/lower_intrinsics.c @@ -805,15 +805,35 @@ int i_mapper_strncmp(ir_node *call, void *ctx) { return 0; } /* i_mapper_strncmp */ +/* A mapper for strcpy */ +int i_mapper_strcpy(ir_node *call, void *ctx) { + ir_node *dst = get_Call_param(call, 0); + ir_node *src = get_Call_param(call, 1); + (void) ctx; + + if (dst == src) { + /* a strcpy(d, s) ==> d */ + ir_node *mem = get_Call_mem(call); + ir_node *dst = get_Call_param(call, 0); + + DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_STRCPY); + replace_call(dst, call, mem, NULL, NULL); + return 1; + } + return 0; +} /* i_mapper_strcpy */ + /* A mapper for memcpy */ int i_mapper_memcpy(ir_node *call, void *ctx) { + ir_node *dst = get_Call_param(call, 0); + ir_node *src = get_Call_param(call, 1); ir_node *len = get_Call_param(call, 2); (void) ctx; - if (is_Const(len) && is_Const_null(len)) { + if (dst == src || (is_Const(len) && is_Const_null(len))) { + /* a memcpy(d, d, len) ==> d OR /* a memcpy(d, s, 0) ==> d */ ir_node *mem = get_Call_mem(call); - ir_node *dst = get_Call_param(call, 0); DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMCPY); replace_call(dst, call, mem, NULL, NULL); @@ -822,6 +842,48 @@ int i_mapper_memcpy(ir_node *call, void *ctx) { return 0; } /* i_mapper_memcpy */ +/* A mapper for mempcpy */ +int i_mapper_mempcpy(ir_node *call, void *ctx) { + ir_node *dst = get_Call_param(call, 0); + ir_node *src = get_Call_param(call, 1); + ir_node *len = get_Call_param(call, 2); + (void) ctx; + + if (dst == src || (is_Const(len) && is_Const_null(len))) { + /* a memcpy(d, d, len) ==> d + len OR + /* a memcpy(d, s, 0) ==> d + 0 */ + dbg_info *dbg = get_irn_dbg_info(call); + ir_node *mem = get_Call_mem(call); + ir_node *blk = get_nodes_block(call); + ir_mode *mode = get_irn_mode(dst); + ir_node *res = new_rd_Add(dbg, get_irn_irg(blk), blk, dst, len, mode); + + DBG_OPT_ALGSIM0(call, res, FS_OPT_RTS_MEMPCPY); + replace_call(res, call, mem, NULL, NULL); + return 1; + } + return 0; +} /* i_mapper_mempcpy */ + +/* A mapper for memmove */ +int i_mapper_memmove(ir_node *call, void *ctx) { + ir_node *dst = get_Call_param(call, 0); + ir_node *src = get_Call_param(call, 1); + ir_node *len = get_Call_param(call, 2); + (void) ctx; + + if (dst == src || (is_Const(len) && is_Const_null(len))) { + /* a memmove(d, d, len) ==> d OR + /* a memmove(d, s, 0) ==> d */ + ir_node *mem = get_Call_mem(call); + + DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMMOVE); + replace_call(dst, call, mem, NULL, NULL); + return 1; + } + return 0; +} /* i_mapper_memmove */ + /* A mapper for memset */ int i_mapper_memset(ir_node *call, void *ctx) { ir_node *len = get_Call_param(call, 2); @@ -839,6 +901,32 @@ int i_mapper_memset(ir_node *call, void *ctx) { return 0; } /* i_mapper_memset */ +/* A mapper for memcmp */ +int i_mapper_memcmp(ir_node *call, void *ctx) { + ir_node *left = get_Call_param(call, 0); + ir_node *right = get_Call_param(call, 1); + ir_node *len = get_Call_param(call, 2); + ir_node *irn; + (void) ctx; + + if (left == right || (is_Const(len) && is_Const_null(len))) { + /* a memcmp(s, s, len) ==> 0 OR + a memcmp(a, b, 0) ==> 0 */ + ir_node *mem = get_Call_mem(call); + ir_node *adr = get_Call_ptr(call); + ir_entity *ent = get_SymConst_entity(adr); + ir_type *call_tp = get_entity_type(ent); + ir_type *res_tp = get_method_res_type(call_tp, 0); + ir_mode *mode = get_type_mode(res_tp); + + irn = new_Const(get_mode_null(mode)); + DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP); + replace_call(irn, call, mem, NULL, NULL); + return 1; + } + return 0; +} /* i_mapper_memcmp */ + /** * Returns the result mode of a node. */ diff --git a/ir/stat/stat_dmp.c b/ir/stat/stat_dmp.c index b3dad262c..64554985a 100644 --- a/ir/stat/stat_dmp.c +++ b/ir/stat/stat_dmp.c @@ -157,9 +157,13 @@ static const struct { { FS_OPT_RTS_SYMMETRIC, "RTS optimization: call to symmetric function f(-x) replaced by f(x)" }, { FS_OPT_RTS_STRCMP, "RTS optimization: call to strcmp() replaced" }, { FS_OPT_RTS_STRNCMP, "RTS optimization: call to strncmp() replaced" }, + { FS_OPT_RTS_STRCPY, "RTS optimization: call to strcpy() replaced" }, + { FS_OPT_RTS_STRLEN, "RTS optimization: call to strlen() replaced" }, { FS_OPT_RTS_MEMCPY, "RTS optimization: call to memcpy() replaced" }, + { FS_OPT_RTS_MEMPCPY, "RTS optimization: call to mempcpy() replaced" }, + { FS_OPT_RTS_MEMMOVE, "RTS optimization: call to memmove() replaced" }, { FS_OPT_RTS_MEMSET, "RTS optimization: call to memset() replaced" }, - { FS_OPT_RTS_STRLEN, "RTS optimization: call to strlen() replaced" }, + { FS_OPT_RTS_MEMCMP, "RTS optimization: call to memcmp() replaced" }, { FS_BE_IA32_LEA, "ia32 Backend transformation: Lea was created" }, { FS_BE_IA32_LOAD_LEA, "ia32 Backend transformation: Load merged with a Lea" }, { FS_BE_IA32_STORE_LEA, "ia32 Backend transformation: Store merged with a Lea" },