From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1971) id 1C9E33857407; Thu, 13 Oct 2022 11:11:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C9E33857407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665659514; bh=3Y2aIjZKdUSHw5RiuPjaHxcVR2FXDJ2SHYhVt3fTJ4A=; h=From:To:Subject:Date:From; b=vkWMWUgNWUbotCq4DbSdjzF4oLRJWUp8TCaK7c1RiATQYhYdR9d7tEH5OsfLNeeIu 7wL6iAYbEr4QQqMVtvEt1n1L6M5fOR2R65G9Yq2xQ4FWYj0wB9qSkvT6BfQ0dxh2D+ CeI9yzoUljmQaeAAfi31f2PVsGSZAlAh+7BoxEhw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Andre Simoes Dias Vieira To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3270] ifcvt: Fix bitpos calculation in bitfield lowering [PR107229] X-Act-Checkin: gcc X-Git-Author: Andre Vieira X-Git-Refname: refs/heads/master X-Git-Oldrev: 6f653a2c85a450714bac905ea4f153234bc6cd5f X-Git-Newrev: 9f0d4adabe2035886a1aa8d2ca990a90de000613 Message-Id: <20221013111154.1C9E33857407@sourceware.org> Date: Thu, 13 Oct 2022 11:11:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9f0d4adabe2035886a1aa8d2ca990a90de000613 commit r13-3270-g9f0d4adabe2035886a1aa8d2ca990a90de000613 Author: Andre Vieira Date: Thu Oct 13 12:09:38 2022 +0100 ifcvt: Fix bitpos calculation in bitfield lowering [PR107229] The bitposition calculation for the bitfield lowering in loop if conversion was not taking DECL_FIELD_OFFSET into account, which meant that it would result in wrong bitpositions for bitfields that did not end up having representations starting at the beginning of the struct. gcc/ChangeLog: PR tree-optimization/107229 * tree-if-conv.cc (get_bitfield_rep): Fix bitposition calculation. gcc/testsuite/ChangeLog: * gcc.dg/vect/pr107229-1.c: New test. * gcc.dg/vect/pr107229-2.c: New test. * gcc.dg/vect/pr107229-3.c: New test. Diff: --- gcc/testsuite/gcc.dg/vect/pr107229-1.c | 16 ++++++++++++++++ gcc/testsuite/gcc.dg/vect/pr107229-2.c | 18 ++++++++++++++++++ gcc/testsuite/gcc.dg/vect/pr107229-3.c | 19 +++++++++++++++++++ gcc/tree-if-conv.cc | 32 ++++++++++++++++++++++++++++---- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/pr107229-1.c b/gcc/testsuite/gcc.dg/vect/pr107229-1.c new file mode 100644 index 00000000000..67b432383d0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr107229-1.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* PR tree-optimization/107229. */ + +int a, c; +struct { + long d; + int : 8; + int : 27; + int e : 21; +} f; +void g(int b) { a = a & 1; } +int main() { + while (c) + g(f.e); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/vect/pr107229-2.c b/gcc/testsuite/gcc.dg/vect/pr107229-2.c new file mode 100644 index 00000000000..88bffb63d5e --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr107229-2.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* PR tree-optimization/107229. */ + +int a, c; +struct { + long f; + long g; + long d; + int : 8; + int : 27; + int e : 21; +} f; +void g(int b) { a = a & 1; } +int main() { + while (c) + g(f.e); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/vect/pr107229-3.c b/gcc/testsuite/gcc.dg/vect/pr107229-3.c new file mode 100644 index 00000000000..4abd8c14531 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/pr107229-3.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* PR tree-optimization/107229. */ + +int a, c; +struct { + long f; + long g; + long d; + int : 8; + int : 32; + int : 2; + int e : 21; +} f; +void g(int b) { a = a & 1; } +int main() { + while (c) + g(f.e); + return 0; +} diff --git a/gcc/tree-if-conv.cc b/gcc/tree-if-conv.cc index e468a4659fa..01637c5da08 100644 --- a/gcc/tree-if-conv.cc +++ b/gcc/tree-if-conv.cc @@ -3298,10 +3298,34 @@ get_bitfield_rep (gassign *stmt, bool write, tree *bitpos, *struct_expr = TREE_OPERAND (comp_ref, 0); if (bitpos) - *bitpos - = fold_build2 (MINUS_EXPR, bitsizetype, - DECL_FIELD_BIT_OFFSET (field_decl), - DECL_FIELD_BIT_OFFSET (rep_decl)); + { + /* To calculate the bitposition of the BITFIELD_REF we have to determine + where our bitfield starts in relation to the container REP_DECL. The + DECL_FIELD_OFFSET of the original bitfield's member FIELD_DECL tells + us how many bytes from the start of the structure there are until the + start of the group of bitfield members the FIELD_DECL belongs to, + whereas DECL_FIELD_BIT_OFFSET will tell us how many bits from that + position our actual bitfield member starts. For the container + REP_DECL adding DECL_FIELD_OFFSET and DECL_FIELD_BIT_OFFSET will tell + us the distance between the start of the structure and the start of + the container, though the first is in bytes and the later other in + bits. With this in mind we calculate the bit position of our new + BITFIELD_REF by subtracting the number of bits between the start of + the structure and the container from the number of bits from the start + of the structure and the actual bitfield member. */ + tree bf_pos = fold_build2 (MULT_EXPR, bitsizetype, + DECL_FIELD_OFFSET (field_decl), + build_int_cst (bitsizetype, BITS_PER_UNIT)); + bf_pos = fold_build2 (PLUS_EXPR, bitsizetype, bf_pos, + DECL_FIELD_BIT_OFFSET (field_decl)); + tree rep_pos = fold_build2 (MULT_EXPR, bitsizetype, + DECL_FIELD_OFFSET (rep_decl), + build_int_cst (bitsizetype, BITS_PER_UNIT)); + rep_pos = fold_build2 (PLUS_EXPR, bitsizetype, rep_pos, + DECL_FIELD_BIT_OFFSET (rep_decl)); + + *bitpos = fold_build2 (MINUS_EXPR, bitsizetype, bf_pos, rep_pos); + } return rep_decl;