Ping. https://gcc.gnu.org/pipermail/gcc-patches/2026-November/738025.html
Thanks, Kyrill > On 9 Nov 2026, at 15:37, Kyrylo Tkachov <[email protected]> wrote: > > From: Kyrylo Tkachov <[email protected]> > > When update_range_test replaces an operand by a range test on the operand > itself, it inserts the new test after the definition of the operand. > That is not possible if the definition ends its basic block: a call that > can throw while a C++ destructor is live, a call in a function that > calls a returns_twice function, or with -fnon-call-exceptions a > comparison that can trap: > > _13 = fa_11(D) > fb_12(D); > > The new statement then follows the definition in the same block, and > checking fails with "statement marked for throw in middle of block" or > "control flow in the middle of basic block". Without checking, release > compilers crash later in ehcleanup or store-merging. GCC 15 has the > same bug. > > Treat such a definition like a default definition: use the operand > itself if it is the range test, and give up otherwise. > > Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu. > Ok for trunk? > Thanks, > Kyrill > > gcc/ChangeLog: > > * tree-ssa-reassoc.cc (update_range_test): Treat a definition of > OP that ends its basic block like a default definition. > > gcc/testsuite/ChangeLog: > > * g++.dg/torture/reassoc-ends-bb-1.C: New test. > * g++.dg/torture/reassoc-ends-bb-2.C: New test. > * gcc.c-torture/compile/reassoc-ends-bb-1.c: New test. > > Signed-off-by: Kyrylo Tkachov <[email protected]> > --- > .../g++.dg/torture/reassoc-ends-bb-1.C | 21 ++++++++++++++++ > .../g++.dg/torture/reassoc-ends-bb-2.C | 24 +++++++++++++++++++ > .../gcc.c-torture/compile/reassoc-ends-bb-1.c | 20 ++++++++++++++++ > gcc/tree-ssa-reassoc.cc | 11 +++++---- > 4 files changed, 72 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/torture/reassoc-ends-bb-1.C > create mode 100644 gcc/testsuite/g++.dg/torture/reassoc-ends-bb-2.C > create mode 100644 gcc/testsuite/gcc.c-torture/compile/reassoc-ends-bb-1.c > > diff --git a/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-1.C > b/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-1.C > new file mode 100644 > index 00000000000..5369acf8192 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-1.C > @@ -0,0 +1,21 @@ > +// A call that can throw ends its basic block. A range test on its > +// result must not be inserted after it. > +// { dg-do compile } > + > +struct S { ~S (); }; > +bool cmp (); > + > +unsigned > +f () > +{ > + S guard; > + unsigned s = 0; > + int m = 0; > + bool c = cmp (); > + for (int i = 0; i < 4; i++) > + { > + m = -(int) c; > + s = s * 5u + (m ? 0u : 3u); > + } > + return s + (m != -1 || c); > +} > diff --git a/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-2.C > b/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-2.C > new file mode 100644 > index 00000000000..8a5fbc19a28 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/torture/reassoc-ends-bb-2.C > @@ -0,0 +1,24 @@ > +// With -fnon-call-exceptions a comparison that can trap ends its basic > +// block. A range test on its result must not be inserted after it. > +// { dg-do compile } > +// { dg-additional-options "-fnon-call-exceptions" } > + > +struct S { ~S (); }; > + > +unsigned > +f (float a, float b) > +{ > + unsigned s = 0; > + int m = 0; > + bool c; > + { > + S guard; > + c = a > b; > + } > + for (int i = 0; i < 4; i++) > + { > + m = -(int) c; > + s = s * 5u + (m ? 0u : 3u); > + } > + return s + (m != -1 || c); > +} > diff --git a/gcc/testsuite/gcc.c-torture/compile/reassoc-ends-bb-1.c > b/gcc/testsuite/gcc.c-torture/compile/reassoc-ends-bb-1.c > new file mode 100644 > index 00000000000..038eacd4a52 > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/compile/reassoc-ends-bb-1.c > @@ -0,0 +1,20 @@ > +/* A call that can make an abnormal goto ends its basic block. A range > + test on its result must not be inserted after it. */ > + > +int rt (void) __attribute__ ((returns_twice)); > +_Bool cmp (void); > + > +unsigned > +f (void) > +{ > + unsigned s = 0; > + int m = 0; > + rt (); > + _Bool c = cmp (); > + for (int i = 0; i < 4; i++) > + { > + m = -(int) c; > + s = s * 5u + (m ? 0u : 3u); > + } > + return s + (m != -1 || c); > +} > diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc > index 63a37c38a4b..125b7ab7dbb 100644 > --- a/gcc/tree-ssa-reassoc.cc > +++ b/gcc/tree-ssa-reassoc.cc > @@ -2867,10 +2867,13 @@ update_range_test (struct range_entry *range, struct > range_entry *otherrange, > if (tem == NULL_TREE) > return false; > > - /* If op is default def SSA_NAME, there is no place to insert the > - new comparison. Give up, unless we can use OP itself as the > - range test. */ > - if (op && SSA_NAME_IS_DEFAULT_DEF (op)) > + /* If op is default def SSA_NAME, or the range test on OP itself would > + have to follow a definition of OP that ends its basic block, there > + is no place to insert the new comparison. Give up, unless we can > + use OP itself as the range test. */ > + if (op > + && (SSA_NAME_IS_DEFAULT_DEF (op) > + || (op == range->exp && stmt_ends_bb_p (stmt)))) > { > if (op == range->exp > && ((TYPE_PRECISION (optype) == 1 && TYPE_UNSIGNED (optype)) > -- > 2.54.0 (Apple Git-157) >
