diff options
author | Martin Sebor <msebor@redhat.com> | 2020-08-14 17:11:53 -0600 |
---|---|---|
committer | Martin Sebor <msebor@redhat.com> | 2020-08-14 17:11:53 -0600 |
commit | 866626efd749ed3e2b7014e88e4340b5a4c73560 (patch) | |
tree | 9a97cbd204e7f85f1d5388fd61e95f4e6a2ab9ad /gcc/fold-const.c | |
parent | 2867118ddda9b56d991c16022f7d3d634ed08313 (diff) | |
download | gcc-866626efd749ed3e2b7014e88e4340b5a4c73560.zip gcc-866626efd749ed3e2b7014e88e4340b5a4c73560.tar.gz gcc-866626efd749ed3e2b7014e88e4340b5a4c73560.tar.bz2 |
PR tree-optimization/78257 - missing memcmp optimization with constant arrays
gcc/ChangeLog:
PR middle-end/78257
* builtins.c (expand_builtin_memory_copy_args): Rename called function.
(expand_builtin_stpcpy_1): Remove argument from call.
(expand_builtin_memcmp): Rename called function.
(inline_expand_builtin_bytecmp): Same.
* expr.c (convert_to_bytes): New function.
(constant_byte_string): New function (formerly string_constant).
(string_constant): Call constant_byte_string.
(byte_representation): New function.
* expr.h (byte_representation): Declare.
* fold-const-call.c (fold_const_call): Rename called function.
* fold-const.c (c_getstr): Remove an argument.
(getbyterep): Define a new function.
* fold-const.h (c_getstr): Remove an argument.
(getbyterep): Declare a new function.
* gimple-fold.c (gimple_fold_builtin_memory_op): Rename callee.
(gimple_fold_builtin_string_compare): Same.
(gimple_fold_builtin_memchr): Same.
gcc/testsuite/ChangeLog:
PR middle-end/78257
* gcc.dg/memchr.c: New test.
* gcc.dg/memcmp-2.c: New test.
* gcc.dg/memcmp-3.c: New test.
* gcc.dg/memcmp-4.c: New test.
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r-- | gcc/fold-const.c | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 5d27927..9fc4c2a 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -15485,19 +15485,19 @@ fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off) ptr, size_int (off)); } -/* Return a pointer P to a NUL-terminated string containing the sequence +/* Return a pointer to a NUL-terminated string containing the sequence of bytes corresponding to the representation of the object referred to by SRC (or a subsequence of such bytes within it if SRC is a reference to an initialized constant array plus some constant offset). - If STRSIZE is non-null, store the number of bytes in the constant - sequence including the terminating NUL byte. *STRSIZE is equal to - sizeof(A) - OFFSET where A is the array that stores the constant - sequence that SRC points to and OFFSET is the byte offset of SRC from - the beginning of A. SRC need not point to a string or even an array - of characters but may point to an object of any type. */ + Set *STRSIZE the number of bytes in the constant sequence including + the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET + where A is the array that stores the constant sequence that SRC points + to and OFFSET is the byte offset of SRC from the beginning of A. SRC + need not point to a string or even an array of characters but may point + to an object of any type. */ const char * -c_getstr (tree src, unsigned HOST_WIDE_INT *strsize /* = NULL */) +getbyterep (tree src, unsigned HOST_WIDE_INT *strsize) { /* The offset into the array A storing the string, and A's byte size. */ tree offset_node; @@ -15506,7 +15506,10 @@ c_getstr (tree src, unsigned HOST_WIDE_INT *strsize /* = NULL */) if (strsize) *strsize = 0; - src = string_constant (src, &offset_node, &mem_size, NULL); + if (strsize) + src = byte_representation (src, &offset_node, &mem_size, NULL); + else + src = string_constant (src, &offset_node, &mem_size, NULL); if (!src) return NULL; @@ -15574,6 +15577,18 @@ c_getstr (tree src, unsigned HOST_WIDE_INT *strsize /* = NULL */) return offset < init_bytes ? string + offset : ""; } +/* Return a pointer to a NUL-terminated string corresponding to + the expression STR referencing a constant string, possibly + involving a constant offset. Return null if STR either doesn't + reference a constant string or if it involves a nonconstant + offset. */ + +const char * +c_getstr (tree str) +{ + return getbyterep (str, NULL); +} + /* Given a tree T, compute which bits in T may be nonzero. */ wide_int |