public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-5815] lower-bitint: Fix up -fnon-call-exceptions bit-field load lowering [PR112668]
@ 2023-11-24  8:02 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-11-24  8:02 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-5815-g9a96a9e45b421436ca93efadba0f1901f12fb6c0
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Nov 24 08:54:40 2023 +0100

    lower-bitint: Fix up -fnon-call-exceptions bit-field load lowering [PR112668]
    
    As the following testcase shows, there are some bugs in the
    -fnon-call-exceptions bit-field load lowering.  In particular, there
    is a case where we want to emit a load early in the initialization
    (before m_init_gsi) and because that load might throw exception, need
    to split block after the load so that it has an EH edge.
    Now, across this splitting, we have m_init_gsi, save_gsi (something
    we put back into m_gsi afterwards) statement iterators and m_preheader_bb
    which is used to determine the pre-header edge of a loop (if any).
    As the testcase shows, both of these statement iterators and m_preheader_bb
    as well need adjustments if the block was split.  If the stmt iterators
    refer to a statement, they need to be updated so that if the statement is
    in the bb after the split gsi_bb and gsi_seq is updated, otherwise they
    ought to be the start of the new (second) bb.
    Similarly, m_preheader_bb should be updated to the second bb if it was
    the first before.  Other spots where we insert something before m_init_gsi
    don't split blocks in there and are fine.
    
    The m_gsi iterator is normal iterator to insert statements before it,
    so gsi_end_p means insert statements at the end of basic block.
    m_init_gsi is on the other side an iterator after which statements should be
    inserted (so gsi_end_p means insert statements at the start of basic block
    after labels), but the whole pass is written for insertion of statements before
    iterators, so when in 3 spots it wants to insert something after m_init_gsi,
    it saves current iterator to save_gsi and sets m_gsi to gsi_after_labels
    if m_init_gsi was gsi_end_p, or to the next statement.  But it actually wasn't
    updating m_init_gsi back when switching to normal iterator, this patch changes
    that such that further statements after m_init_gsi will appear after the
    set of statements inserted before m_init_gsi.
    
    Finally, the pass had a couple of places where it wanted to create a gsi_end_p
    iterator for a particular basic block, instead of doing
    m_gsi = gsi_last_bb (bb); if (!gsi_end_p (m_gsi)) gsi_next (&m_gsi);
    the pass now uses new m_gsi = gsi_end_bb (bb) function.
    
    2023-11-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/112668
            * gimple-iterator.h (gsi_end, gsi_end_bb): New inline functions.
            * gimple-lower-bitint.cc (bitint_large_huge::handle_cast): After
            temporarily adding statements after m_init_gsi, update m_init_gsi
            such that later additions after it will be after the added statements.
            (bitint_large_huge::handle_load): Likewise.  When splitting
            gsi_bb (m_init_gsi) basic block, update m_preheader_bb if needed
            and update saved m_gsi as well if needed.
            (bitint_large_huge::lower_mergeable_stmt,
            bitint_large_huge::lower_comparison_stmt,
            bitint_large_huge::lower_mul_overflow,
            bitint_large_huge::lower_bit_query): Use gsi_end_bb.
    
            * gcc.dg/bitint-40.c: New test.

Diff:
---
 gcc/gimple-iterator.h            | 35 ++++++++++++++++++++++++++++
 gcc/gimple-lower-bitint.cc       | 50 ++++++++++++++++++++++++----------------
 gcc/testsuite/gcc.dg/bitint-40.c | 29 +++++++++++++++++++++++
 3 files changed, 94 insertions(+), 20 deletions(-)

diff --git a/gcc/gimple-iterator.h b/gcc/gimple-iterator.h
index b709923f00d..a7a80773a91 100644
--- a/gcc/gimple-iterator.h
+++ b/gcc/gimple-iterator.h
@@ -169,6 +169,41 @@ gsi_last_bb (basic_block bb)
   return i;
 }
 
+/* Return a new iterator pointing to before the first statement or after
+   last statement (depending on whether adding statements after it or before it)
+   in a GIMPLE_SEQ.  */
+
+inline gimple_stmt_iterator
+gsi_end (gimple_seq &seq)
+{
+  gimple_stmt_iterator i;
+  gimple *g = gimple_seq_last (seq);
+
+  i.ptr = NULL;
+  i.seq = &seq;
+  i.bb = g ? gimple_bb (g) : NULL;
+
+  return i;
+}
+
+/* Return a new iterator pointing to before the first statement or after
+   last statement (depending on whether adding statements after it or before it)
+   in basic block BB.  */
+
+inline gimple_stmt_iterator
+gsi_end_bb (basic_block bb)
+{
+  gimple_stmt_iterator i;
+  gimple_seq *seq;
+
+  seq = bb_seq_addr (bb);
+  i.ptr = NULL;
+  i.seq = seq;
+  i.bb = bb;
+
+  return i;
+}
+
 /* Return true if I is at the end of its sequence.  */
 
 inline bool
diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index c429cb245d3..1ec2579282c 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -1294,6 +1294,11 @@ bitint_large_huge::handle_cast (tree lhs_type, tree rhs1, tree idx)
 	      g = gimple_build_assign (n, RSHIFT_EXPR, t, lpm1);
 	      insert_before (g);
 	      m_data[save_data_cnt + 1] = add_cast (m_limb_type, n);
+	      m_init_gsi = m_gsi;
+	      if (gsi_end_p (m_init_gsi))
+		m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
+	      else
+		gsi_prev (&m_init_gsi);
 	      m_gsi = save_gsi;
 	    }
 	  else if (m_upwards_2limb * limb_prec < TYPE_PRECISION (rhs_type))
@@ -1523,6 +1528,11 @@ bitint_large_huge::handle_cast (tree lhs_type, tree rhs1, tree idx)
 	      insert_before (g);
 	      rext = add_cast (m_limb_type, gimple_assign_lhs (g));
 	    }
+	  m_init_gsi = m_gsi;
+	  if (gsi_end_p (m_init_gsi))
+	    m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
+	  else
+	    gsi_prev (&m_init_gsi);
 	  m_gsi = save_gsi;
 	}
       tree t;
@@ -1687,9 +1697,23 @@ bitint_large_huge::handle_load (gimple *stmt, tree idx)
 		      edge e = split_block (gsi_bb (m_gsi), g);
 		      make_edge (e->src, eh_edge->dest, EDGE_EH)->probability
 			= profile_probability::very_unlikely ();
-		      m_init_gsi.bb = e->dest;
+		      m_gsi = gsi_after_labels (e->dest);
+		      if (gsi_bb (save_gsi) == e->src)
+			{
+			  if (gsi_end_p (save_gsi))
+			    save_gsi = gsi_end_bb (e->dest);
+			  else
+			    save_gsi = gsi_for_stmt (gsi_stmt (save_gsi));
+			}
+		      if (m_preheader_bb == e->src)
+			m_preheader_bb = e->dest;
 		    }
 		}
+	      m_init_gsi = m_gsi;
+	      if (gsi_end_p (m_init_gsi))
+		m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
+	      else
+		gsi_prev (&m_init_gsi);
 	      m_gsi = save_gsi;
 	      tree out;
 	      prepare_data_in_out (iv, idx, &out);
@@ -2359,11 +2383,7 @@ bitint_large_huge::lower_mergeable_stmt (gimple *stmt, tree_code &cmp_code,
       edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
       edge_bb = e->src;
       if (kind == bitint_prec_large)
-	{
-	  m_gsi = gsi_last_bb (edge_bb);
-	  if (!gsi_end_p (m_gsi))
-	    gsi_next (&m_gsi);
-	}
+	m_gsi = gsi_end_bb (edge_bb);
     }
   else
     m_after_stmt = stmt;
@@ -2816,9 +2836,7 @@ bitint_large_huge::lower_comparison_stmt (gimple *stmt, tree_code &cmp_code,
   gsi_prev (&gsi);
   edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
   edge_bb = e->src;
-  m_gsi = gsi_last_bb (edge_bb);
-  if (!gsi_end_p (m_gsi))
-    gsi_next (&m_gsi);
+  m_gsi = gsi_end_bb (edge_bb);
 
   edge *edges = XALLOCAVEC (edge, cnt * 2);
   for (unsigned i = 0; i < cnt; i++)
@@ -4288,9 +4306,7 @@ bitint_large_huge::lower_mul_overflow (tree obj, gimple *stmt)
 	  gsi_prev (&gsi);
 	  edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
 	  edge_bb = e->src;
-	  m_gsi = gsi_last_bb (edge_bb);
-	  if (!gsi_end_p (m_gsi))
-	    gsi_next (&m_gsi);
+	  m_gsi = gsi_end_bb (edge_bb);
 
 	  tree cmp = build_zero_cst (m_limb_type);
 	  for (unsigned i = 0; i < cnt; i++)
@@ -4560,11 +4576,7 @@ bitint_large_huge::lower_bit_query (gimple *stmt)
 	  edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
 	  edge_bb = e->src;
 	  if (kind == bitint_prec_large)
-	    {
-	      m_gsi = gsi_last_bb (edge_bb);
-	      if (!gsi_end_p (m_gsi))
-		gsi_next (&m_gsi);
-	    }
+	    m_gsi = gsi_end_bb (edge_bb);
 	  bqp = XALLOCAVEC (struct bq_details, cnt);
 	}
       else
@@ -4717,9 +4729,7 @@ bitint_large_huge::lower_bit_query (gimple *stmt)
       gsi_prev (&gsi);
       edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
       edge_bb = e->src;
-      m_gsi = gsi_last_bb (edge_bb);
-      if (!gsi_end_p (m_gsi))
-	gsi_next (&m_gsi);
+      m_gsi = gsi_end_bb (edge_bb);
 
       if (ifn == IFN_CLZ)
 	bqp = XALLOCAVEC (struct bq_details, cnt);
diff --git a/gcc/testsuite/gcc.dg/bitint-40.c b/gcc/testsuite/gcc.dg/bitint-40.c
new file mode 100644
index 00000000000..e4185097ed3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bitint-40.c
@@ -0,0 +1,29 @@
+/* PR middle-end/112668 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c23 -fnon-call-exceptions" } */
+
+#if __BITINT_MAXWIDTH__ >= 156
+struct T156 { _BitInt(156) a : 2; unsigned _BitInt(156) b : 135; _BitInt(156) c : 2; };
+extern void foo156 (struct T156 *);
+
+unsigned _BitInt(156)
+bar156 (int i)
+{
+  struct T156 r156[12];
+  foo156 (&r156[0]);
+  return r156[i].b;
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 495
+struct T495 { _BitInt(495) a : 2; unsigned _BitInt(495) b : 471; _BitInt(495) c : 2; };
+extern void foo495 (struct T495 *r495);
+
+unsigned _BitInt(495)
+bar495 (int i)
+{
+  struct T495 r495[12];
+  foo495 (r495);
+  return r495[i].b;
+}
+#endif

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-24  8:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-24  8:02 [gcc r14-5815] lower-bitint: Fix up -fnon-call-exceptions bit-field load lowering [PR112668] Jakub Jelinek

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).