public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-8956] tree-optimization/113895 - copy_reference_ops_from_ref vs. bitfields
Date: Tue, 13 Feb 2024 12:42:29 +0000 (GMT)	[thread overview]
Message-ID: <20240213124229.87A88385828F@sourceware.org> (raw)

https://gcc.gnu.org/g:94225dfb5623725fa519eac69338f7a632a509ae

commit r14-8956-g94225dfb5623725fa519eac69338f7a632a509ae
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Feb 13 11:10:57 2024 +0100

    tree-optimization/113895 - copy_reference_ops_from_ref vs. bitfields
    
    The recent enhancement to discover constant array indices by range
    info used by get_ref_base_and_extent doesn't work when the outermost
    component reference is to a bitfield because we track the running
    offset in the reference ops as bytes.  The following does as
    ao_ref_init_from_vn_reference and recovers that manually, tracking
    the offset for the purpose of discovering the constant array index
    in bits instead.
    
            PR tree-optimization/113895
            * tree-ssa-sccvn.cc (copy_reference_ops_from_ref): Track
            offset to discover constant array indices in bits, handle
            COMPONENT_REF to bitfields.
    
            * gcc.dg/torture/pr113895-1.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr113895-1.c | 16 ++++++++++++++++
 gcc/tree-ssa-sccvn.cc                     | 26 +++++++++++++++++++++++---
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr113895-1.c b/gcc/testsuite/gcc.dg/torture/pr113895-1.c
new file mode 100644
index 000000000000..e96cb2f33e16
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr113895-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+
+int main_i;
+void transparent_crc(int);
+#pragma pack(1)
+struct {
+  signed : 17;
+  signed : 6;
+  unsigned : 13;
+  unsigned f6 : 12;
+} g_20[1];
+int main()
+{
+  transparent_crc(g_20[main_i].f6);
+  return 0;
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 95670ae2ed6d..d6b8c734e7ba 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -1119,14 +1119,14 @@ copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
 	      unsigned HOST_WIDE_INT elsz
 		= tree_to_uhwi (op.op2) * vn_ref_op_align_unit (&op);
 	      unsigned HOST_WIDE_INT idx
-		= (coffset / BITS_PER_UNIT - off.to_constant ()) / elsz;
+		= (coffset - off.to_constant ()) / BITS_PER_UNIT / elsz;
 	      if (idx == 0)
 		op.op0 = op.op1;
 	      else
 		op.op0 = wide_int_to_tree (TREE_TYPE (op.op0),
 					   wi::to_poly_wide (op.op1) + idx);
 	      op.off = idx * elsz;
-	      off += op.off;
+	      off += op.off * BITS_PER_UNIT;
 	    }
 	  else
 	    {
@@ -1140,10 +1140,30 @@ copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
 		       || TREE_CODE_CLASS (op.opcode) == tcc_constant)
 		/* end-of ref.  */
 		gcc_assert (i == result->length ());
+	      else if (op.opcode == COMPONENT_REF)
+		{
+		  /* op.off is tracked in bytes, re-do it manually
+		     because of bitfields.  */
+		  tree field = op.op0;
+		  /* We do not have a complete COMPONENT_REF tree here so we
+		     cannot use component_ref_field_offset.  Do the interesting
+		     parts manually.  */
+		  tree this_offset = DECL_FIELD_OFFSET (field);
+		  if (op.op1 || !poly_int_tree_p (this_offset))
+		    gcc_unreachable ();
+		  else
+		    {
+		      poly_offset_int woffset
+			= (wi::to_poly_offset (this_offset)
+			   << LOG2_BITS_PER_UNIT);
+		      woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
+		      off += woffset.force_shwi ();
+		    }
+		}
 	      else
 		{
 		  gcc_assert (known_ne (op.off, -1));
-		  off += op.off;
+		  off += op.off * BITS_PER_UNIT;
 		}
 	    }
 	}

                 reply	other threads:[~2024-02-13 12:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240213124229.87A88385828F@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).