Bug 85974 - [8 Regression] Failure to optimize difference of two pointers into a compile time constant
Summary: [8 Regression] Failure to optimize difference of two pointers into a compile ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.0
: P2 minor
Target Milestone: 8.2
Assignee: Richard Biener
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2018-05-29 13:33 UTC by pkoning
Modified: 2018-07-16 11:02 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 8.1.1, 9.0
Known to fail: 8.1.0
Last reconfirmed: 2018-05-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description pkoning 2018-05-29 13:33:57 UTC
This issue is exposed by test case testsuite/gcc.c-torture/compile/930326-1.c, on platforms where ptrdiff_t is not "long" (such as pdp11).  In that case, the last line:
   long i = s.f-&s.b;
fails with "error: initializer element is not computable at load time".

It's not a target bug; the problem can be reproduced for other targets by changing the "long" to "char" in that statement.
Comment 1 Marc Glisse 2018-05-29 14:19:28 UTC
In match.pd

 (simplify
- (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
+ (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))

(that is, we can have only one cast, not just 0 or 2)

and similarly for the adjacent transformation. I didn't check if that requires any other adjustment, just pointing out a likely place for a patch.
Comment 2 Richard Biener 2018-05-30 10:55:39 UTC
Confirmed.
Comment 3 Richard Biener 2018-07-12 16:41:55 UTC
Mine.
Comment 4 Martin Sebor 2018-07-12 19:32:24 UTC
The optimization aside, the code in the test violates the C constraint that:

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; ...

Since s.f and s.b and distinct arrays (with s.b being treated as an array of one element), the behavior of the test case is undefined.  GCC should diagnose it before some optimization relies on code not doing these kinds of bad things (as we have seen recently in bug 86259).

A valid (and more straightforward) way of writing the same code is:

  struct S
  {
    char a, b, f[3];
  } s;

  long i = offsetof (struct S, f) - offsetof (struct S, b);

or (for the purposes of testing):

  long i = ((char*)&s + offsetof (struct S, f)) - (char*)&s + offsetof (struct S, b));
Comment 5 Richard Biener 2018-07-13 11:26:03 UTC
Fixed on trunk sofar.
Comment 6 Richard Biener 2018-07-13 11:26:12 UTC
Author: rguenth
Date: Fri Jul 13 11:25:38 2018
New Revision: 262632

URL: https://gcc.gnu.org/viewcvs?rev=262632&root=gcc&view=rev
Log:
2018-07-13  Richard Biener  <rguenther@suse.de>

	PR middle-end/85974
	* match.pd (addr1 - addr2): Allow either of the operand to
	have a conversion.

	* gcc.c-torture/compile/930326-1.c: Adjust to cover widening.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.c-torture/compile/930326-1.c
Comment 7 Richard Biener 2018-07-16 11:02:24 UTC
Author: rguenth
Date: Mon Jul 16 11:01:48 2018
New Revision: 262690

URL: https://gcc.gnu.org/viewcvs?rev=262690&root=gcc&view=rev
Log:
2018-07-16  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2018-07-13  Richard Biener  <rguenther@suse.de>

	PR middle-end/85974
	* match.pd (addr1 - addr2): Allow either of the operand to
	have a conversion.

	* gcc.c-torture/compile/930326-1.c: Adjust to cover widening.

	2018-06-15  Richard Biener  <rguenther@suse.de>

	PR middle-end/86076
	* tree-cfg.c (move_stmt_op): unshare invariant addresses
	before adjusting their block.

	* gcc.dg/pr86076.c: New testcase.

	2018-06-06  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/85935
	* graphite-scop-detection.c (find_params_in_bb): Analyze
	condition operands with respect to the correct loop.  Assert
	the analysis doesn't fail.

	* gcc.dg/graphite/pr85935.c: New testcase.

Added:
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/graphite/pr85935.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/pr86076.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/graphite-scop-detection.c
    branches/gcc-8-branch/gcc/match.pd
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
    branches/gcc-8-branch/gcc/testsuite/gcc.c-torture/compile/930326-1.c
    branches/gcc-8-branch/gcc/tree-cfg.c
Comment 8 Richard Biener 2018-07-16 11:02:47 UTC
Fixed.