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.
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.
Confirmed.
Mine.
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));
Fixed on trunk sofar.
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
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
Fixed.