diff options
author | Siddhesh Poyarekar <siddhesh@gotplt.org> | 2021-11-15 23:03:15 +0530 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@gotplt.org> | 2021-11-16 04:20:46 +0530 |
commit | 323026c7dfe23e1093e80f7db5f4851d1a867b62 (patch) | |
tree | 50e43bf3d1fb56115469cc2cf6fbb859015267a3 /gcc/testsuite | |
parent | cea4dab861bae6536dd5655a42d73f2c17f655f7 (diff) | |
download | gcc-323026c7dfe23e1093e80f7db5f4851d1a867b62.zip gcc-323026c7dfe23e1093e80f7db5f4851d1a867b62.tar.gz gcc-323026c7dfe23e1093e80f7db5f4851d1a867b62.tar.bz2 |
gimple-fold: Use ranges to simplify strncat and snprintf
Use ranges for lengths and object sizes in strncat and snprintf to
determine if they can be transformed into simpler operations.
gcc/ChangeLog:
* gimple-fold.c (gimple_fold_builtin_strncat): Use ranges to
determine if it is safe to transform to strcat.
(gimple_fold_builtin_snprintf): Likewise.
gcc/testsuite/ChangeLog:
* gcc.dg/fold-stringops-2.c: Define size_t.
(safe1): Adjust.
(safe4): New test.
* gcc.dg/fold-stringops-3.c: New test.
Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
Diffstat (limited to 'gcc/testsuite')
-rw-r--r-- | gcc/testsuite/gcc.dg/fold-stringops-2.c | 16 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/fold-stringops-3.c | 18 |
2 files changed, 33 insertions, 1 deletions
diff --git a/gcc/testsuite/gcc.dg/fold-stringops-2.c b/gcc/testsuite/gcc.dg/fold-stringops-2.c index 0b415df..ac7d29e 100644 --- a/gcc/testsuite/gcc.dg/fold-stringops-2.c +++ b/gcc/testsuite/gcc.dg/fold-stringops-2.c @@ -1,10 +1,12 @@ /* { dg-do compile } */ /* { dg-options "-O2" } */ +typedef __SIZE_TYPE__ size_t; + #define bos(__d) __builtin_object_size ((__d), 0) char * -safe1 (const char *src, int cond, __SIZE_TYPE__ len) +safe1 (const char *src, int cond, size_t len) { char *dst; @@ -44,6 +46,18 @@ safe3 (const char *src, int cond, unsigned char len) return __builtin___snprintf_chk (dst, len, 0, bos (dst), "%s", src); } +char dst[1024]; + +void +safe4 (size_t len) +{ + len = len > sizeof (dst) - 1 ? sizeof (dst) - 1 : len; + len = len < sizeof (dst) / 2 ? sizeof (dst) / 2 : len; + + __builtin_strncat (dst, "hello", len); +} + /* { dg-final { scan-assembler-not "__memcpy_chk" } } */ /* { dg-final { scan-assembler-not "__strncpy_chk" } } */ /* { dg-final { scan-assembler-not "__snprintf_chk" } } */ +/* { dg-final { scan-assembler-not "strncat" } } */ diff --git a/gcc/testsuite/gcc.dg/fold-stringops-3.c b/gcc/testsuite/gcc.dg/fold-stringops-3.c new file mode 100644 index 0000000..ae2efbf --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-stringops-3.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +typedef __SIZE_TYPE__ size_t; + +char dst[1024]; + +void +safe1 (size_t len) +{ + len = len > sizeof (dst) ? sizeof (dst) : len; + len = len < sizeof (dst) / 2 ? sizeof (dst) / 2 : len; + + __builtin_snprintf (dst, len, "hello"); + __builtin_snprintf (dst + 5, len, "%s", " world"); +} + +/* { dg-final { scan-assembler-not "snprintf" } } */ |