From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1059) id A3256385F01D; Thu, 19 Mar 2020 12:39:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3256385F01D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1584621570; bh=o0u0scHVuDr+q6+aHdMg8BNcrlBlcbaYMUA8esfe32g=; h=From:To:Subject:Date:From; b=chKMdt6ChpG+U0oI4UN+03+Mos2WnempXpli0yo4jN2g+nT3hZkSHmZ+oZCedPUGq CKray3gggomZrux9ZB2sqc+d0Z297bcEEi+Kjfr8eCS3C9EA302OgVh4AbIZBdi5PS zUWDgPlVnb9xVO15Le0XlVpjaLqDRiAD4gzNWhsQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Nathan Sidwell To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/c++-modules] fold undefined pointer offsetting X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/devel/c++-modules X-Git-Oldrev: d564c5e254df744a470a658690753dc193a4fa78 X-Git-Newrev: cb99630f254aaec6591e0a200b79905b31d24eb3 Message-Id: <20200319123930.A3256385F01D@sourceware.org> Date: Thu, 19 Mar 2020 12:39:30 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Mar 2020 12:39:30 -0000 https://gcc.gnu.org/g:cb99630f254aaec6591e0a200b79905b31d24eb3 commit cb99630f254aaec6591e0a200b79905b31d24eb3 Author: Richard Biener Date: Wed Mar 11 15:34:47 2020 +0100 fold undefined pointer offsetting This avoids breaking the old broken pointer offsetting via (T)(ptr - ((T)0)->x) which should have used offsetof. Breakage was exposed by the introduction of POINTER_DIFF_EXPR and making PTA not considering that producing a pointer. The mitigation for simple cases is to canonicalize _2 = _1 - 8B; o_9 = (struct obj *) _2; to o_9 = &MEM[_1 + -8B]; eliding one statement and the offending pointer subtraction. 2020-03-11 Richard Biener * match.pd ((T *)(ptr - ptr-cst) -> &MEM[ptr + -ptr-cst]): New pattern. * gcc.dg/torture/20200311-1.c: New testcase. Diff: --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 9 +++++++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/torture/20200311-1.c | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 0a79692a53f..73339dc2c2c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2020-03-11 Richard Biener + + * match.pd ((T *)(ptr - ptr-cst) -> &MEM[ptr + -ptr-cst]): + New pattern. + 2020-03-11 Eric Botcazou PR middle-end/93961 diff --git a/gcc/match.pd b/gcc/match.pd index 19df0c404a4..9cb37740f1e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1864,6 +1864,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (ptr_difference_const (@0, @1, &diff)) { build_int_cst_type (type, diff); })))) +/* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */ +(simplify + (convert (pointer_diff @0 INTEGER_CST@1)) + (if (POINTER_TYPE_P (type)) + { build_fold_addr_expr_with_type + (build2 (MEM_REF, char_type_node, @0, + wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))), + type); })) + /* If arg0 is derived from the address of an object or function, we may be able to fold this expression using the object or function's alignment. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0e8d77ab9dd..11061adaf18 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2020-03-11 Richard Biener + + * gcc.dg/torture/20200311-1.c: New testcase. + 2020-03-11 Matthew Malcomson * lib/scanasm.exp (parse_function_bodies): Lines starting with '@' also diff --git a/gcc/testsuite/gcc.dg/torture/20200311-1.c b/gcc/testsuite/gcc.dg/torture/20200311-1.c new file mode 100644 index 00000000000..ac82b6b7a0b --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/20200311-1.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ + +struct list { struct list *n; }; + +struct obj { + int n; + struct list l; +} _o; + +struct list _l = { .n = &_o.l }; + +int main(int argc, char *argv[]) +{ + struct obj *o = &_o; + _o.l.n = &_l; + while (&o->l != &_l) + /* Note the following is invoking undefined behavior but in + this kind of "obvious" cases we don't want to break things + unnecessarily and thus we avoid analyzing o as pointing + to nothing via the undefined pointer subtraction. Instead + we canonicalize the pointer subtraction followed by the + pointer conversion to pointer offsetting. */ + o = ((struct obj *)((const char *)(o->l.n) + - (const char *)&((struct obj *)0)->l)); + return 0; +}