[gcc r14-8186] lower-bitint: Avoid overlap between destinations and sources in libgcc calls [PR113421]

Jakub Jelinek jakub@gcc.gnu.org
Wed Jan 17 13:44:25 GMT 2024


https://gcc.gnu.org/g:9f8ba332e988fb582f6ca32465f6d65283f53b3a

commit r14-8186-g9f8ba332e988fb582f6ca32465f6d65283f53b3a
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 17 14:43:40 2024 +0100

    lower-bitint: Avoid overlap between destinations and sources in libgcc calls [PR113421]
    
    The following testcase is miscompiled because the bitint lowering emits a
      .MULBITINT (&a, 1024, &a, 1024, &x, 1024);
    call.  The bug is in the overlap between the destination and source, that is
    something the libgcc routines don't handle, they use the source arrays
    during the entire algorithms which computes the destination array(s).
    For the mapping of SSA_NAMEs to VAR_DECLs the code already supports that
    correctly, but the checking whether a load from memory can be used directly
    without a temporary even when earlier we decided to merge the
    multiplication/division/modulo etc. with a store didn't.
    
    The following patch implements that.
    
    2024-01-17  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/113421
            * gimple-lower-bitint.cc (stmt_needs_operand_addr): Adjust function
            comment.
            (bitint_dom_walker::before_dom_children): Add g temporary to simplify
            formatting.  Start at vop rather than cvop even if stmt is a store
            and needs_operand_addr.
    
            * gcc.dg/torture/bitint-50.c: New test.

Diff:
---
 gcc/gimple-lower-bitint.cc               | 13 +++++++------
 gcc/testsuite/gcc.dg/torture/bitint-50.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index d3d113578d9..e48125daec9 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -5455,7 +5455,8 @@ vuse_eq (ao_ref *, tree vuse1, void *data)
 
 /* Return true if STMT uses a library function and needs to take
    address of its inputs.  We need to avoid bit-fields in those
-   cases.  */
+   cases.  Similarly, we need to avoid overlap between destination
+   and source limb arrays.  */
 
 bool
 stmt_needs_operand_addr (gimple *stmt)
@@ -5574,7 +5575,8 @@ bitint_dom_walker::before_dom_children (basic_block bb)
 	  else if (!bitmap_bit_p (m_loads, SSA_NAME_VERSION (s)))
 	    continue;
 
-	  tree rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s));
+	  gimple *g = SSA_NAME_DEF_STMT (s);
+	  tree rhs1 = gimple_assign_rhs1 (g);
 	  if (needs_operand_addr
 	      && TREE_CODE (rhs1) == COMPONENT_REF
 	      && DECL_BIT_FIELD_TYPE (TREE_OPERAND (rhs1, 1)))
@@ -5596,15 +5598,14 @@ bitint_dom_walker::before_dom_children (basic_block bb)
 
 	  ao_ref ref;
 	  ao_ref_init (&ref, rhs1);
-	  tree lvop = gimple_vuse (SSA_NAME_DEF_STMT (s));
+	  tree lvop = gimple_vuse (g);
 	  unsigned limit = 64;
 	  tree vuse = cvop;
 	  if (vop != cvop
 	      && is_gimple_assign (stmt)
 	      && gimple_store_p (stmt)
-	      && !operand_equal_p (lhs,
-				   gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s)),
-				   0))
+	      && (needs_operand_addr
+		  || !operand_equal_p (lhs, gimple_assign_rhs1 (g), 0)))
 	    vuse = vop;
 	  if (vuse != lvop
 	      && walk_non_aliased_vuses (&ref, vuse, false, vuse_eq,
diff --git a/gcc/testsuite/gcc.dg/torture/bitint-50.c b/gcc/testsuite/gcc.dg/torture/bitint-50.c
new file mode 100644
index 00000000000..c148a2dafa1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/bitint-50.c
@@ -0,0 +1,31 @@
+/* PR tree-optimization/113421 */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+/* { dg-skip-if "" { ! run_expensive_tests }  { "*" } { "-O0" "-O2" } } */
+/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */
+
+#if __BITINT_MAXWIDTH__ >= 1024
+unsigned _BitInt(1024) a = -5wb;
+
+__attribute__((noipa)) void
+foo (unsigned _BitInt(1024) x)
+{
+  a *= x;
+}
+#else
+int a = 30;
+
+void
+foo (int)
+{
+}
+#endif
+
+int
+main ()
+{
+  foo (-6wb);
+  if (a != 30wb)
+    __builtin_abort ();
+  return 0;
+}


More information about the Gcc-cvs mailing list