* [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass.
@ 2022-02-20 16:51 Roger Sayle
2022-03-02 19:32 ` Jeff Law
2022-05-12 15:27 ` Jeff Law
0 siblings, 2 replies; 4+ messages in thread
From: Roger Sayle @ 2022-02-20 16:51 UTC (permalink / raw)
To: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 1249 bytes --]
This patch implements the missed optimization enhancement PR 83907,
by handling memset with a constant byte value in tree-ssa's strlen
optimization pass. Effectively, this treats memset(dst,'x',3) as
it would memcpy(dst,"xxx",3).
This patch also includes a tweak to handle_store to address another
missed optimization observed in the related test case pr83907-2.c.
The consecutive byte stores to memory get coalesced into a vector
write of a vector const, but unfortunately tree-ssa-strlen's
handle_store didn't previously handle the (unusual) case where the
stored "string" starts with a zero byte but also contains non-zero
bytes.
This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check with no new failures. Ok for mainline?
2022-02-20 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
PR tree-optimization/83907
* tree-ssa-strlen.cc (handle_builtin_memset): Record a strinfo
for memset with an constant char value.
(handle_store): Improved handling of stores with a first byte
of zero, but not storing_all_zeros_p.
gcc/testsuite/ChangeLog
PR tree-optimization/83907
* gcc.dg/tree-ssa/pr83907-1.c: New test case.
* gcc.dg/tree-ssa/pr83907-2.c: New test case.
Thanks in advance,
Roger
--
[-- Attachment #2: patchslb.txt --]
[-- Type: text/plain, Size: 4462 bytes --]
diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc
index 7370516..d2db197 100644
--- a/gcc/tree-ssa-strlen.cc
+++ b/gcc/tree-ssa-strlen.cc
@@ -3810,9 +3810,44 @@ strlen_pass::handle_builtin_memset (bool *zero_write)
{
gimple *memset_stmt = gsi_stmt (m_gsi);
tree ptr = gimple_call_arg (memset_stmt, 0);
+ tree memset_val = gimple_call_arg (memset_stmt, 1);
+ tree memset_size = gimple_call_arg (memset_stmt, 2);
+
/* Set to the non-constant offset added to PTR. */
wide_int offrng[2];
int idx1 = get_stridx (ptr, memset_stmt, offrng, ptr_qry.rvals);
+ if (idx1 == 0
+ && TREE_CODE (memset_val) == INTEGER_CST
+ && ((TREE_CODE (memset_size) == INTEGER_CST
+ && !integer_zerop (memset_size))
+ || TREE_CODE (memset_size) == SSA_NAME))
+ {
+ unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
+ bool full_string_p = (wi::to_wide (memset_val) & mask) == 0;
+
+ /* We only handle symbolic lengths when writing non-zero values. */
+ if (full_string_p && TREE_CODE (memset_size) != INTEGER_CST)
+ return false;
+
+ idx1 = new_stridx (ptr);
+ if (idx1 == 0)
+ return false;
+ tree newlen;
+ if (full_string_p)
+ newlen = build_int_cst (size_type_node, 0);
+ else if (TREE_CODE (memset_size) == INTEGER_CST)
+ newlen = fold_convert (size_type_node, memset_size);
+ else
+ newlen = memset_size;
+
+ strinfo *dsi = new_strinfo (ptr, idx1, newlen, full_string_p);
+ set_strinfo (idx1, dsi);
+ find_equal_ptrs (ptr, idx1);
+ dsi->dont_invalidate = true;
+ dsi->writable = true;
+ return false;
+ }
+
if (idx1 <= 0)
return false;
strinfo *si1 = get_strinfo (idx1);
@@ -3825,7 +3860,6 @@ strlen_pass::handle_builtin_memset (bool *zero_write)
if (!valid_builtin_call (alloc_stmt))
return false;
tree alloc_size = gimple_call_arg (alloc_stmt, 0);
- tree memset_size = gimple_call_arg (memset_stmt, 2);
/* Check for overflow. */
maybe_warn_overflow (memset_stmt, false, memset_size, NULL, false, true);
@@ -3841,7 +3875,7 @@ strlen_pass::handle_builtin_memset (bool *zero_write)
return false;
/* Bail when the call writes a non-zero value. */
- if (!integer_zerop (gimple_call_arg (memset_stmt, 1)))
+ if (!integer_zerop (memset_val))
return false;
/* Let the caller know the memset call cleared the destination. */
@@ -5093,8 +5127,9 @@ strlen_pass::handle_store (bool *zero_write)
return false;
}
- if (storing_all_zeros_p
- || storing_nonzero_p
+ if (storing_nonzero_p
+ || storing_all_zeros_p
+ || (full_string_p && lenrange[1] == 0)
|| (offset != 0 && store_before_nul[1] > 0))
{
/* When STORING_NONZERO_P, we know that the string will start
@@ -5104,8 +5139,9 @@ strlen_pass::handle_store (bool *zero_write)
of leading non-zero characters and set si->NONZERO_CHARS to
the result instead.
- When STORING_ALL_ZEROS_P, we know that the string is now
- OFFSET characters long.
+ When STORING_ALL_ZEROS_P, or the first byte written is zero,
+ i.e. FULL_STRING_P && LENRANGE[1] == 0, we know that the
+ string is now OFFSET characters long.
Otherwise, we're storing an unknown value at offset OFFSET,
so need to clip the nonzero_chars to OFFSET.
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83907-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83907-1.c
new file mode 100644
index 0000000..2a6f4f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83907-1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+extern char str[];
+
+unsigned int foo()
+{
+ __builtin_memset(str,'x',5);
+ str[5] = 0;
+ return __builtin_strlen (str);
+}
+
+/* { dg-final { scan-tree-dump-not "strlen" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83907-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83907-2.c
new file mode 100644
index 0000000..cc27504
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83907-2.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+extern char str[];
+
+unsigned int foo()
+{
+ __builtin_memset(str,'x',5);
+ str[5] = 0;
+ str[6] = 'z';
+ return __builtin_strlen (str);
+}
+
+/* { dg-final { scan-tree-dump-not "strlen" "optimized" } } */
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass.
2022-02-20 16:51 [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass Roger Sayle
@ 2022-03-02 19:32 ` Jeff Law
2022-05-12 7:14 ` Roger Sayle
2022-05-12 15:27 ` Jeff Law
1 sibling, 1 reply; 4+ messages in thread
From: Jeff Law @ 2022-03-02 19:32 UTC (permalink / raw)
To: Roger Sayle, gcc-patches
On 2/20/2022 9:51 AM, Roger Sayle wrote:
> This patch implements the missed optimization enhancement PR 83907,
> by handling memset with a constant byte value in tree-ssa's strlen
> optimization pass. Effectively, this treats memset(dst,'x',3) as
> it would memcpy(dst,"xxx",3).
>
> This patch also includes a tweak to handle_store to address another
> missed optimization observed in the related test case pr83907-2.c.
> The consecutive byte stores to memory get coalesced into a vector
> write of a vector const, but unfortunately tree-ssa-strlen's
> handle_store didn't previously handle the (unusual) case where the
> stored "string" starts with a zero byte but also contains non-zero
> bytes.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check with no new failures. Ok for mainline?
>
>
> 2022-02-20 Roger Sayle <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
> PR tree-optimization/83907
> * tree-ssa-strlen.cc (handle_builtin_memset): Record a strinfo
> for memset with an constant char value.
> (handle_store): Improved handling of stores with a first byte
> of zero, but not storing_all_zeros_p.
>
> gcc/testsuite/ChangeLog
> PR tree-optimization/83907
> * gcc.dg/tree-ssa/pr83907-1.c: New test case.
> * gcc.dg/tree-ssa/pr83907-2.c: New test case.
Deferring to stage1.
jeff
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass.
2022-03-02 19:32 ` Jeff Law
@ 2022-05-12 7:14 ` Roger Sayle
0 siblings, 0 replies; 4+ messages in thread
From: Roger Sayle @ 2022-05-12 7:14 UTC (permalink / raw)
To: 'Jeff Law'; +Cc: gcc-patches
Hi Jeff,
Any chance you could take a look at this patch, now that we're back in stage1?
Thanks in advance,
Roger
> -----Original Message-----
> From: Jeff Law <jeffreyalaw@gmail.com>
> Sent: 02 March 2022 19:33
> To: Roger Sayle <roger@nextmovesoftware.com>; gcc-patches@gcc.gnu.org
> Subject: Re: [PATCH] PR tree-optimization/83907: Improved memset handling in
> strlen pass.
>
> On 2/20/2022 9:51 AM, Roger Sayle wrote:
> > This patch implements the missed optimization enhancement PR 83907, by
> > handling memset with a constant byte value in tree-ssa's strlen
> > optimization pass. Effectively, this treats memset(dst,'x',3) as it
> > would memcpy(dst,"xxx",3).
> >
> > This patch also includes a tweak to handle_store to address another
> > missed optimization observed in the related test case pr83907-2.c.
> > The consecutive byte stores to memory get coalesced into a vector
> > write of a vector const, but unfortunately tree-ssa-strlen's
> > handle_store didn't previously handle the (unusual) case where the
> > stored "string" starts with a zero byte but also contains non-zero
> > bytes.
> >
> > This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> > and make -k check with no new failures. Ok for mainline?
> >
> >
> > 2022-02-20 Roger Sayle <roger@nextmovesoftware.com>
> >
> > gcc/ChangeLog
> > PR tree-optimization/83907
> > * tree-ssa-strlen.cc (handle_builtin_memset): Record a strinfo
> > for memset with an constant char value.
> > (handle_store): Improved handling of stores with a first byte
> > of zero, but not storing_all_zeros_p.
> >
> > gcc/testsuite/ChangeLog
> > PR tree-optimization/83907
> > * gcc.dg/tree-ssa/pr83907-1.c: New test case.
> > * gcc.dg/tree-ssa/pr83907-2.c: New test case.
> Deferring to stage1.
> jeff
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass.
2022-02-20 16:51 [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass Roger Sayle
2022-03-02 19:32 ` Jeff Law
@ 2022-05-12 15:27 ` Jeff Law
1 sibling, 0 replies; 4+ messages in thread
From: Jeff Law @ 2022-05-12 15:27 UTC (permalink / raw)
To: gcc-patches
On 2/20/2022 9:51 AM, Roger Sayle wrote:
> This patch implements the missed optimization enhancement PR 83907,
> by handling memset with a constant byte value in tree-ssa's strlen
> optimization pass. Effectively, this treats memset(dst,'x',3) as
> it would memcpy(dst,"xxx",3).
>
> This patch also includes a tweak to handle_store to address another
> missed optimization observed in the related test case pr83907-2.c.
> The consecutive byte stores to memory get coalesced into a vector
> write of a vector const, but unfortunately tree-ssa-strlen's
> handle_store didn't previously handle the (unusual) case where the
> stored "string" starts with a zero byte but also contains non-zero
> bytes.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check with no new failures. Ok for mainline?
>
>
> 2022-02-20 Roger Sayle <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
> PR tree-optimization/83907
> * tree-ssa-strlen.cc (handle_builtin_memset): Record a strinfo
> for memset with an constant char value.
> (handle_store): Improved handling of stores with a first byte
> of zero, but not storing_all_zeros_p.
>
> gcc/testsuite/ChangeLog
> PR tree-optimization/83907
> * gcc.dg/tree-ssa/pr83907-1.c: New test case.
> * gcc.dg/tree-ssa/pr83907-2.c: New test case.
OK after a re-test. Thanks for the ping.
jeff
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-05-12 15:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-20 16:51 [PATCH] PR tree-optimization/83907: Improved memset handling in strlen pass Roger Sayle
2022-03-02 19:32 ` Jeff Law
2022-05-12 7:14 ` Roger Sayle
2022-05-12 15:27 ` Jeff Law
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).