From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1285) id EA44B3858C66; Wed, 11 Jan 2023 19:46:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA44B3858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673466395; bh=SaTlR09KejhV/YlAuE5WgGBDN30/mzDrtQt9jSHUNuw=; h=From:To:Subject:Date:From; b=p9jjQDh2v+IAXMRK+QCu7vZHdnS1xzNNckXDPr/+v0Su8Q82OPiD4qXKRClwiT3pK 4j6y5v/IidhaU5T3igbwnNV7Z+HrpiR0cQfr2l6W4jAqcV0f5ccf/wdLb0tz8LYxzg dsoCKLpzSqNRIPQzk9/V4caub1h+0gnRox87qyLc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Eric Botcazou To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9041] Fix problematic interaction between bitfields, unions, SSO and SRA X-Act-Checkin: gcc X-Git-Author: Eric Botcazou X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: bd4c310b06d747975853ac6dfef6da120c13f6ec X-Git-Newrev: eec3a65ed638a1c58fa08ddf508d2d60b64d311d Message-Id: <20230111194635.EA44B3858C66@sourceware.org> Date: Wed, 11 Jan 2023 19:46:35 +0000 (GMT) List-Id: https://gcc.gnu.org/g:eec3a65ed638a1c58fa08ddf508d2d60b64d311d commit r12-9041-geec3a65ed638a1c58fa08ddf508d2d60b64d311d Author: Eric Botcazou Date: Wed Jan 11 15:58:47 2023 +0100 Fix problematic interaction between bitfields, unions, SSO and SRA The handling of bitfields by the SRA pass is peculiar and this must be taken into account to support the scalar_storage_order attribute. gcc/ PR tree-optimization/108199 * tree-sra.cc (sra_modify_expr): Deal with reverse storage order for bit-field references. gcc/testsuite/ * gcc.dg/sso-17.c: New test. Diff: --- gcc/testsuite/gcc.dg/sso-17.c | 52 +++++++++++++++++++++++++++++++++++++++++++ gcc/tree-sra.cc | 18 ++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/gcc.dg/sso-17.c b/gcc/testsuite/gcc.dg/sso-17.c new file mode 100644 index 00000000000..67e2d3793fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/sso-17.c @@ -0,0 +1,52 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define REV_ENDIANNESS __attribute__((scalar_storage_order("big-endian"))) +#else +#define REV_ENDIANNESS __attribute__((scalar_storage_order("little-endian"))) +#endif + +typedef unsigned long long u64; + +union DST { + u64 val; + + struct { + u64 x : 1; + u64 y : 1; + u64 r: 62; + } REV_ENDIANNESS; +} REV_ENDIANNESS; + + +struct SRC { + u64 a; +} REV_ENDIANNESS; + +[[gnu::noipa]] +void foo () {__builtin_abort();} + +[[gnu::noinline]] +int bar(struct SRC *src) +{ + union DST dst; + + dst.val = src->a; + + if (dst.y) { + foo(); + } + return 0; +} + +int main(void) +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + struct SRC t = {-1ull & (~(0x01ull<<62))}; +#else + struct SRC t = {-1ull & (~(0x01ull<<1))}; +#endif + bar(&t); + return 0; +} diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc index 95291e8df93..09dbd7be77f 100644 --- a/gcc/tree-sra.cc +++ b/gcc/tree-sra.cc @@ -3858,7 +3858,23 @@ sra_modify_expr (tree *expr, gimple_stmt_iterator *gsi, bool write) } } else - *expr = repl; + { + /* If we are going to replace a scalar field in a structure with + reverse storage order by a stand-alone scalar, we are going to + effectively byte-swap the scalar and we also need to byte-swap + the portion of it represented by the bit-field. */ + if (bfr && REF_REVERSE_STORAGE_ORDER (bfr)) + { + REF_REVERSE_STORAGE_ORDER (bfr) = 0; + TREE_OPERAND (bfr, 2) + = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (repl)), + size_binop (PLUS_EXPR, TREE_OPERAND (bfr, 1), + TREE_OPERAND (bfr, 2))); + } + + *expr = repl; + } + sra_stats.exprs++; } else if (write && access->grp_to_be_debug_replaced)