Bug 79449 - ppc builtin expansion of strncmp can cross page (4k) boundary where it should not
Summary: ppc builtin expansion of strncmp can cross page (4k) boundary where it should...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: acsawdey
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2017-02-10 03:17 UTC by acsawdey
Modified: 2017-03-27 15:40 UTC (History)
2 users (show)

See Also:
Host:
Target: powerpc64*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description acsawdey 2017-02-10 03:17:18 UTC
When one of the string args to strncmp has alignment of less than 8 bytes, the builtin expansion code generates a runtime check to see if the string might cross a 4k boundary. However the runtime check looks for at least the length N before the next 4k boundary and then the code generated may load a doubleword, crossing the 4k boundary if N is less than 8.
Comment 1 acsawdey 2017-02-13 16:00:54 UTC
Author: acsawdey
Date: Mon Feb 13 16:00:22 2017
New Revision: 245392

URL: https://gcc.gnu.org/viewcvs?rev=245392&root=gcc&view=rev
Log:
2017-02-13  Aaron Sawdey  <acsawdey@linux.vnet.ibm.com>

	PR target/79449
	* gcc.dg/strncmp-2.c: New.  Test strncmp and memcmp builtin expansion
	for reading beyond a 4k boundary.

2017-02-13  Aaron Sawdey  <acsawdey@linux.vnet.ibm.com>

	PR target/79449
	* config/rs6000/rs6000.c (expand_block_compare): Make sure runtime
	boundary crossing check and subsequent code generation agree.



Added:
    trunk/gcc/testsuite/gcc.dg/strncmp-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/rs6000/rs6000.c
    trunk/gcc/testsuite/ChangeLog
Comment 2 acsawdey 2017-02-13 17:24:39 UTC
Fix checked in to 245392.
Comment 3 acsawdey 2017-03-27 15:40:52 UTC
Author: acsawdey
Date: Mon Mar 27 15:40:20 2017
New Revision: 246504

URL: https://gcc.gnu.org/viewcvs?rev=246504&root=gcc&view=rev
Log:
2017-03-27  Aaron Sawdey  <acsawdey@linux.vnet.ibm.com>

	Backport from trunk
	PR target/79449
	PR target/79170

	* gcc.dg/strncmp-2.c: New.  Test strncmp and memcmp builtin expansion
	for reading beyond a 4k boundary.
	* config/rs6000/rs6000.c (expand_block_compare): Make sure runtime
	boundary crossing check and subsequent code generation agree.
	* gcc.dg/memcmp-1.c: Improved to catch failures seen in PR 79170.
	* config/rs6000/altivec.md (*setb_internal): Rename to setb_signed.
	(setb_unsigned) New pattern for setb with CCUNS.
	* config/rs6000/rs6000.c (expand_block_compare): Use a different
	subfc./subfe sequence to avoid overflow problems.  Generate a
	shorter sequence with cmpld/setb for power9.
	* config/rs6000/rs6000.md (subf<mode>3_carry_dot2): Add a new pattern
	for generating subfc. instruction.
	(cmpstrsi): Add TARGET_POPCNTD predicate as the generate sequence
	now uses this instruction.
	* config/rs6000/rs6000-protos.h (expand_strn_compare): Add arg.
	* config/rs6000/rs6000.c (expand_strn_compare): Add ability to expand
	strcmp. Fix bug where comparison didn't stop with zero byte. Fix
	case where N arg is SIZE_MAX.
	* config/rs6000/rs6000.md (cmpstrnsi): Args to expand_strn_compare.
	(cmpstrsi): Add pattern.
	* gcc.dg/memcmp-1.c: New.
	* gcc.dg/strncmp-1.c: New.
	* config/rs6000/rs6000-protos.h (expand_strn_compare): Declare.
	* config/rs6000/rs6000.md (UNSPEC_CMPB): New unspec.
	(cmpb<mode>3): pattern for generating cmpb.
	(cmpstrnsi): pattern to expand strncmp ().
	* config/rs6000/rs6000.opt (mstring-compare-inline-limit): Add a new
	target option for controlling how much code inline expansion of
	strncmp() will be allowed to generate.
	* config/rs6000/rs6000.c (expand_strncmp_align_check): generate code
	for runtime page crossing check of strncmp () args.
	(expand_strn_compare): Function to do builtin expansion of strncmp ().
	* config/i386/i386.md (cmpstrnsi): New test to bail out if neither
	string input is a string constant.
	* builtins.c (expand_builtin_strncmp): Attempt expansion of strncmp
	via cmpstrnsi even if neither string is constant.



Modified:
    branches/ibm/gcc-6-branch/gcc/ChangeLog.ibm
    branches/ibm/gcc-6-branch/gcc/builtins.c
    branches/ibm/gcc-6-branch/gcc/config/i386/i386.md
    branches/ibm/gcc-6-branch/gcc/config/rs6000/altivec.md
    branches/ibm/gcc-6-branch/gcc/config/rs6000/rs6000-protos.h
    branches/ibm/gcc-6-branch/gcc/config/rs6000/rs6000.c
    branches/ibm/gcc-6-branch/gcc/config/rs6000/rs6000.md
    branches/ibm/gcc-6-branch/gcc/config/rs6000/rs6000.opt