From 9a96a9e45b421436ca93efadba0f1901f12fb6c0 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 24 Nov 2023 08:54:40 +0100 Subject: 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 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. --- gcc/gimple-iterator.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'gcc/gimple-iterator.h') diff --git a/gcc/gimple-iterator.h b/gcc/gimple-iterator.h index b709923..a7a8077 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 -- cgit v1.1