Bug 83501 - [8 Regression] strlen(a) not folded after strcpy(a, "...")
Summary: [8 Regression] strlen(a) not folded after strcpy(a, "...")
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: strlen
  Show dependency treegraph
 
Reported: 2017-12-20 00:52 UTC by Martin Sebor
Modified: 2018-06-04 17:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 5.4.0, 6.4.0, 7.2.0
Known to fail: 8.0
Last reconfirmed: 2017-12-20 00:00:00


Attachments
Untested fix (783 bytes, patch)
2017-12-20 07:39 UTC, prathamesh3492
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-12-20 00:52:48 UTC
GCC 7 and prior folded into a constant the strlen call in the following simple test case but GCC 8 no longer does.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
extern char* stpcpy (char*, const char*);
extern char* strcpy (char*, const char*);

char a[4];

void f (void)
{
  strcpy (a, "abc");

  if (__builtin_strlen (a) != 3)
    __builtin_abort ();
}

;; Function f (f, funcdef_no=0, decl_uid=1899, cgraph_uid=0, symbol_order=1)

f ()
{
  long unsigned int _1;

  <bb 2> [local count: 1073741825]:
  MEM[(char * {ref-all})&a] = MEM[(char * {ref-all})"abc"];
  _1 = __builtin_strlen (&a);
  if (_1 != 3)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [99.96%]

  <bb 3> [count: 0]:
  __builtin_abort ();

  <bb 4> [local count: 1073312327]:
  return;

}
Comment 1 Martin Sebor 2017-12-20 00:53:44 UTC
Bisection points to r255197:

2017-11-28  Richard Biener  <rguenther@suse.de>

	PR middle-end/83141
	* gimple-fold.c (gimple_fold_builtin_memory_op): For aggregate
	copies generated from memcpy use a character array as reference
	type.
Comment 2 prathamesh3492 2017-12-20 07:39:22 UTC
Created attachment 42927 [details]
Untested fix
Comment 3 Jakub Jelinek 2017-12-20 11:12:34 UTC
This doesn't look safe.  You are ignoring the second operand of the MEM_REF, if it is non-zero, you'll miscompile stuff.  So, either you need to do it only if integer_zerop (TREE_OPERAND (rhs, 1)) on the MEM_REF, or need to handle the offset (if out of bounds for the strlen, ignore it, otherwise adjust the length accordingly).
Comment 4 Martin Sebor 2017-12-21 01:07:50 UTC
See also bug 81703 for a similar case.
Comment 5 prathamesh3492 2018-01-03 16:08:03 UTC
Author: prathamesh3492
Date: Wed Jan  3 16:07:32 2018
New Revision: 256180

URL: https://gcc.gnu.org/viewcvs?rev=256180&root=gcc&view=rev
Log:
2018-01-03  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR tree-optimization/83501
	* tree-ssa-strlen.c (get_string_cst): New.
	(handle_char_store): Call get_string_cst.

testsuite/
	* gcc.dg/tree-ssa/pr83501.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-strlen.c
Comment 6 prathamesh3492 2018-01-03 16:09:10 UTC
Fixed.
Comment 7 Martin Sebor 2018-01-03 16:27:21 UTC
Author: msebor
Date: Wed Jan  3 16:26:49 2018
New Revision: 256181

URL: https://gcc.gnu.org/viewcvs?rev=256181&root=gcc&view=rev
Log:
PR tree-optimization/83501 - strlen(a) not folded after strcpy(a, "...")

gcc/testsuite/ChangeLog:
	* c-c++-common/Warray-bounds-3.c: Remove xfail.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/Warray-bounds-3.c
Comment 8 prathamesh3492 2018-01-11 04:38:20 UTC
Author: prathamesh3492
Date: Thu Jan 11 04:37:48 2018
New Revision: 256475

URL: https://gcc.gnu.org/viewcvs?rev=256475&root=gcc&view=rev
Log:
2018-01-11  Martin Sebor  <msebor@gmail.com>
	    Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR tree-optimization/83501
	PR tree-optimization/81703

	* tree-ssa-strlen.c (get_string_cst): Rename...
	(get_string_len): ...to this.  Handle global constants.
	(handle_char_store): Adjust.

testsuite/
	* gcc.dg/strlenopt-39.c: New test-case.
	* gcc.dg/pr81703.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/pr81703.c
    trunk/gcc/testsuite/gcc.dg/strlenopt-39.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-strlen.c
Comment 9 prathamesh3492 2018-01-14 08:59:30 UTC
Author: prathamesh3492
Date: Sun Jan 14 08:58:58 2018
New Revision: 256657

URL: https://gcc.gnu.org/viewcvs?rev=256657&root=gcc&view=rev
Log:
2018-01-14  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR tree-optimization/83501
	* gcc.dg/strlenopt-39.c: Restrict to i?86 and x86_64-*-* targets.

Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/strlenopt-39.c