[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-10-19 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #22 from Jonathan Wakely  ---
(In reply to Jakub Jelinek from comment #19)
> I've posted
> https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598896.html
> patch for this, waiting for Jon's review.

N.B. that was committed as r13-2618-g723ef5a937dbab

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-10-19 Thread rguenth at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Richard Biener  changed:

   What|Removed |Added

   Priority|P3  |P2

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-19 Thread rguenth at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Richard Biener  changed:

   What|Removed |Added

   Target Milestone|12.2|12.3

--- Comment #21 from Richard Biener  ---
GCC 12.2 is being released, retargeting bugs to GCC 12.3.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-19 Thread rguenth at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #20 from Richard Biener  ---
See also https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598839.html
and https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598840.html
which I didn't pursue further since they didn't seem to catch the case
reliably.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-19 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #19 from Jakub Jelinek  ---
(In reply to Jason Merrill from comment #17)
> Created attachment 53474 [details]
> patch to work around the issue in the library

I've posted
https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598896.html
patch for this, waiting for Jon's review.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-18 Thread jason at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #18 from Jason Merrill  ---
(In reply to Jason Merrill from comment #17)
> There's probably a way to help the optimizer out without the
> __builtin_unreachable hammer, as for 98465; suggestions are welcome.

..like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104336#c1

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-18 Thread jason at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Jason Merrill  changed:

   What|Removed |Added

 CC||jason at gcc dot gnu.org

--- Comment #17 from Jason Merrill  ---
Created attachment 53474
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53474=edit
patch to work around the issue in the library

This patch tells the optimizer that the copy can't overlap, since it is having
trouble figuring that out on its own.  This fixes the false positive.

It theoretically could deduce this from the previous two conditions: the first
establishes that the end of the source is after the end of the destination; the
second establishes that the beginning of the source is before the end of the
destination.  So the source crosses the end of the destination, and so the
length of the overlap is less than the length of the source.

There's probably a way to help the optimizer out without the
__builtin_unreachable hammer, as for 98465; suggestions are welcome.

Turning off -Wrestrict just around the call to _M_copy also works, but this
patch should also improve optimization.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-08-03 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Jonathan Wakely  changed:

   What|Removed |Added

 CC||hi at jdoubleu dot de

--- Comment #16 from Jonathan Wakely  ---
*** Bug 106512 has been marked as a duplicate of this bug. ***

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread rguenth at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #15 from Richard Biener  ---
Note on trunk we get things way less optimized and end up with

   [local count: 695302893]:
  __x.38_144 = (long unsigned int) " ";
  __y.39_145 = (long unsigned int) _137;
  if (__x.38_144 < __y.39_145)
goto ; [50.00%]
  else
goto ; [50.00%]

   [local count: 347651447]:
  _146 = _137 + _128;
  __x.38_147 = (long unsigned int) _146;
  if (__x.38_147 < __x.38_144)
goto ; [50.00%]
  else
goto ; [50.00%]

and ptr_derefs_may_alias_p doesn't work there because to relate _137 and " "
points-to info would need to track STRING_CSTs but it
doesn't do that (we simply drop knowledge here, see find_what_var_points_to
string_id handling).

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread rguenther at suse dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #14 from rguenther at suse dot de  ---
On Tue, 26 Jul 2022, jakub at gcc dot gnu.org wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651
> 
> --- Comment #12 from Jakub Jelinek  ---
> My preference would be still to make the overlap case out-of-line as mentioned
> in PR105329, but I don't have statistics on whether the overlap case is really
> cold in real-world.  If yes, it would make non-overlap case smaller and the
> overlap case tiny bit slower.

I would say so.  It's of course still beneficial to optimize the
overlap test if possible.  I'm now testing a series of changes required.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #13 from Jonathan Wakely  ---
I think the vast majority of string mutations involve
inserting/appending/substituting an unrelated string, so non-overlapping. We
can make your change on trunk.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

--- Comment #12 from Jakub Jelinek  ---
My preference would be still to make the overlap case out-of-line as mentioned
in PR105329, but I don't have statistics on whether the overlap case is really
cold in real-world.  If yes, it would make non-overlap case smaller and the
overlap case tiny bit slower.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Jonathan Wakely  changed:

   What|Removed |Added

 CC|jwakely at redhat dot com  |redi at gcc dot gnu.org

--- Comment #11 from Jonathan Wakely  ---
(In reply to Richard Biener from comment #10)
> ISTR seeing this before.

PR 105329 maybe?

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-26 Thread rguenth at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Richard Biener  changed:

   What|Removed |Added

   Assignee|unassigned at gcc dot gnu.org  |rguenth at gcc dot 
gnu.org
 Status|NEW |ASSIGNED
 CC||jakub at gcc dot gnu.org,
   ||jwakely at redhat dot com

--- Comment #10 from Richard Biener  ---
ISTR seeing this before.  We are quite confused by the standard library code
comparing the address of a string literal with the local buffer:

MEM[(char_type &) + 17] = 32;
if (_M_local_buf >=   [(void *)"_" + 1B])
  goto ; [50.00%]
else
  goto ; [50.00%]

if (_M_local_buf <= "_")
  goto ; [50.00%]
else
  goto ; [50.00%]

 [local count: 21728216]:
_97 = _M_local_buf - "_";
if (_97 == 1)
  goto ; [34.00%]
else
  goto ; [66.00%]

and we are afraid of optimizing relational compares of distinct objects
(well, that's simply undefined and good QOI is to DWIM, simply leave the
broken code around if there's no good way to choose true or false).

This is all preceeded by an "overlap condition" working on integers
(so not undefined), so what might work is to aggressively simplify
relational compares of known distinct objects to __builtin_unreachable ()
(aka isolate the errorneous path).

 [local count: 1073741824]:
MEM[(struct basic_string *)] ={v} {CLOBBER};
MEM[(struct _Alloc_hider *)] ={v} {CLOBBER};
MEM[(struct _Alloc_hider *)]._M_p = _M_local_buf;
MEM[(char_type &) + 16] = 32;
D.42061._M_string_length = 1;
__x.25_69 = (long unsigned int) "_";
__y.26_70 = (long unsigned int) _M_local_buf;
if (__x.25_69 < __y.26_70)
  goto ; [50.00%]
else
  goto ; [50.00%]

 [local count: 347651448]:
__x.25_72 = (long unsigned int)   [(void *) + 17B];
if (__x.25_69 > __x.25_72)
  goto ; [50.00%]
else
  goto ; [50.00%]

Alternatively somehow re-combine the above overlap test and simplify
it statically.  We are getting into

Breakpoint 5, maybe_fold_and_comparisons (type=, code1=GE_EXPR, op1a=, op1b=, code2=GE_EXPR, op2a=,
op2b=, outer_cond_bb=)

we do have a patterns optimizing an overlap test (but not statically folding)
with

/* For pointers @0 and @2 and nonnegative constant offset @1, look for
   expressions like:

   A: (@0 + @1 < @2) | (@2 + @1 < @0)
   B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
...

unfortunately simple matching in match.pd falls foul of the PR105142 fix
given _72 is computed in the inner block and thus
maybe_fold_comparisons_from_match_pd is not going to pick that up.
Without that fix

(for cmp1 (lt le le lt)
 cmp2 (lt le lt le)
(simplify
 (bit_and (cmp1:c (convert@4 @0) (convert @1)) (cmp2:c (convert@5 @2) (convert
@3)))
 (if (TREE_CODE (@0) == ADDR_EXPR
  && TREE_CODE (@1) == ADDR_EXPR
  && TREE_CODE (@2) == ADDR_EXPR
  && TREE_CODE (@3) == ADDR_EXPR
  && INTEGRAL_TYPE_P (TREE_TYPE (@4))
  && types_match (TREE_TYPE (@4), TREE_TYPE (@5)))
  (with { poly_int64 diff0, diff1; }
   (if (ptr_difference_const (@0, @3, )
&& ptr_difference_const (@1, @2, )
&& !ptr_derefs_may_alias_p (@0, @1))
{ constant_boolean_node (false, type); })

works to detect this for the case of invariant addresses which have the
offset included.  It would need to be extended for cases where the
increments are variable or the base address is not invariant (but common).

I'm going to improve the PR105142 fix.

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-19 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Andrew Pinski  changed:

   What|Removed |Added

 CC||me+gccbugzilla at s5 dot pm

--- Comment #9 from Andrew Pinski  ---
*** Bug 106350 has been marked as a duplicate of this bug. ***

[Bug tree-optimization/105651] [12/13 Regression] bogus "may overlap" memcpy warning with std::string and operator+ at -O3

2022-07-08 Thread marxin at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105651

Martin Liška  changed:

   What|Removed |Added

   Keywords|needs-bisection,|
   |needs-reduction |
 CC||aldyh at gcc dot gnu.org,
   ||marxin at gcc dot gnu.org

--- Comment #8 from Martin Liška  ---
Started with r12-3347-g8af8abfbbace49e6 where new warning appeared:

inlined from ‘std::string f()’ at pr105651.C:5:32:
/dev/shm/gcc-bisect-bin/usr/local/include/c++/12.0.0/bits/char_traits.h:355:56:
error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’
specified bound between 9223372036854775810 and 18446744073709551615 exceeds
maximum object size 9223372036854775807 [-Werror=stringop-overflow=]
  355 | return static_cast(__builtin_memcpy(__s1, __s2,
__n));
  |   
^