copy_reg_eh_region_note_forward and copy_reg_eh_region_note_backward
only copy a REG_EH_REGION note to insns that can throw. Without
-fexceptions that excludes every call, so a nothrow note (0 or INT_MIN)
is dropped whenever peep2_attempt or try_split replaces a call. In a
function with nonlocal labels, a call without an INT_MIN note is
treated as a possible nonlocal goto and ends a basic block, which
rtl_verify_bb_insns rejects:

  error: flow control insn inside a basic block
  internal compiler error: in rtl_verify_bb_insns, at cfgrtl.cc:3011

Seen on alpha-linux with gcc.dg/asan/pr81186.c and pr82545.c at -O2,
where an alpha peephole2 replaces the call.

Copy notes with value 0 or INT_MIN to every new call. Calls emitted
from scratch already get these notes without -fexceptions (from
make_reg_eh_region_note and make_reg_eh_region_note_nothrow_nononlocal),
so this only stops them being lost. With -fexceptions every call can
throw, so nothing changes there.

copy_reg_eh_region_note_backward also now skips insns that already have
a REG_EH_REGION note, as the forward variant does. Otherwise a nothrow
libcall in a split sequence, which is emitted with its own note, would
get a second one.

Suggested-by: Andrea Pinski <[email protected]>

gcc/ChangeLog:

        * except.cc (copy_reg_eh_region_note_p): New function.
        (copy_reg_eh_region_note_forward): Use it.
        (copy_reg_eh_region_note_backward): Likewise. Skip insns that
        already have a REG_EH_REGION note.
---
 gcc/except.cc | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/gcc/except.cc b/gcc/except.cc
index a8985ee81e1..8a9708524b5 100644
--- a/gcc/except.cc
+++ b/gcc/except.cc
@@ -1763,7 +1763,21 @@ insn_could_throw_p (const rtx_insn *insn)
   return false;
 }
 
-/* Copy an REG_EH_REGION note to each insn that might throw beginning
+/* Return true if a REG_EH_REGION note with value NOTE should be copied
+   to INSN.  A nothrow note is copied to every call even if it cannot
+   throw: an INT_MIN note also records that the call cannot perform a
+   nonlocal goto, which matters without -fexceptions too.  */
+
+static bool
+copy_reg_eh_region_note_p (const rtx_insn *insn, rtx note)
+{
+  if (insn_could_throw_p (insn))
+    return true;
+  return (CALL_P (insn)
+         && (INTVAL (note) == 0 || INTVAL (note) == INT_MIN));
+}
+
+/* Copy an REG_EH_REGION note to each insn that needs it, beginning
    at FIRST and ending at LAST.  NOTE_OR_INSN is either the source insn
    to look for a note, or the note itself.  */
 
@@ -1786,7 +1800,7 @@ copy_reg_eh_region_note_forward (rtx note_or_insn, 
rtx_insn *first,
 
   for (rtx_insn *insn = first; insn != last ; insn = NEXT_INSN (insn))
     if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
-        && insn_could_throw_p (insn))
+       && copy_reg_eh_region_note_p (insn, note))
       add_reg_note (insn, REG_EH_REGION, note);
 }
 
@@ -1810,7 +1824,8 @@ copy_reg_eh_region_note_backward (rtx note_or_insn, 
rtx_insn *last,
   note = XEXP (note, 0);
 
   for (rtx_insn *insn = last; insn != first; insn = PREV_INSN (insn))
-    if (insn_could_throw_p (insn))
+    if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
+       && copy_reg_eh_region_note_p (insn, note))
       add_reg_note (insn, REG_EH_REGION, note);
 }
 

-- 
2.56.1

Reply via email to