diff options
author | Steven Bosscher <stevenb@suse.de> | 2004-05-30 21:05:20 +0000 |
---|---|---|
committer | Kazu Hirata <kazu@gcc.gnu.org> | 2004-05-30 21:05:20 +0000 |
commit | b3dd9f3535b30fb5ec664f16aa527bffddf58c6c (patch) | |
tree | b84404b76d9b1fd31876be3c25bee7382aa54383 /gcc | |
parent | 9cf737f89ffe0c75614ad388440b7b7706b6316b (diff) | |
download | gcc-b3dd9f3535b30fb5ec664f16aa527bffddf58c6c.zip gcc-b3dd9f3535b30fb5ec664f16aa527bffddf58c6c.tar.gz gcc-b3dd9f3535b30fb5ec664f16aa527bffddf58c6c.tar.bz2 |
re PR tree-optimization/14819 ([tree-ssa] strchr is not folded at tree-level)
PR tree-optimization/14819
* builtins.c (fold_builtin_strchr): New.
(fold_builtin_1): Handle BUILT_IN_STRCHR and BUILT_IN_STRRCHR
with fold_builtin_strchr().
From-SVN: r82464
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/builtins.c | 47 |
2 files changed, 54 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 98cd593..6fd198e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2004-05-30 Steven Bosscher <stevenb@suse.de> + + PR tree-optimization/14819 + * builtins.c (fold_builtin_strchr): New. + (fold_builtin_1): Handle BUILT_IN_STRCHR and BUILT_IN_STRRCHR + with fold_builtin_strchr(). + 2004-05-30 Kazu Hirata <kazu@cs.umass.edu> * bb-reorder.c, builtins.c, c-common.c, c-gimplify.c, diff --git a/gcc/builtins.c b/gcc/builtins.c index 28eb129..4bac999 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -7085,6 +7085,47 @@ fold_builtin_strncpy (tree exp) return 0; } +/* Fold function call to builtin strchr and strrchr. + Return NULL_TREE if no simplification can be made. */ + +static tree +fold_builtin_strchr (tree exp, bool actually_strrchr) +{ + tree arglist = TREE_OPERAND (exp, 1); + if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) + return 0; + else + { + tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); + const char *p1; + + if (TREE_CODE (s2) != INTEGER_CST) + return 0; + + p1 = c_getstr (s1); + if (p1 != NULL) + { + char c; + const char *r; + + if (target_char_cast (s2, &c)) + return 0; + + r = actually_strrchr ? strrchr (p1, c) : strchr (p1, c); + + if (r == NULL) + return fold_convert (TREE_TYPE (s1), integer_zero_node); + + /* Return an offset into the constant string argument. */ + return fold (build2 (PLUS_EXPR, TREE_TYPE (s1), + s1, fold_convert (TREE_TYPE (s1), + ssize_int (r - p1)))); + } + + return 0; + } +} + /* Fold function call to builtin memcmp. Return NULL_TREE if no simplification can be made. */ @@ -7796,6 +7837,12 @@ fold_builtin_1 (tree exp) case BUILT_IN_STRNCPY: return fold_builtin_strncpy (exp); + case BUILT_IN_STRCHR: + return fold_builtin_strchr (exp, false); + + case BUILT_IN_STRRCHR: + return fold_builtin_strchr (exp, true); + case BUILT_IN_MEMCMP: return fold_builtin_memcmp (exp); |