https://gcc.gnu.org/g:6041d224ab85e557ba219de04e7b5637aeb994b3
commit r17-6461-g6041d224ab85e557ba219de04e7b5637aeb994b3 Author: Kyrylo Tkachov <[email protected]> Date: Wed Oct 14 15:51:27 2026 +0200 reassoc: Do not insert a range test after a statement that ends its block 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. 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]> Diff: --- gcc/testsuite/g++.dg/torture/reassoc-ends-bb-1.C | 21 +++++++++++++++++++ gcc/testsuite/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(-) 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 000000000000..5369acf81926 --- /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 000000000000..8a5fbc19a28f --- /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 000000000000..038eacd4a520 --- /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 dda28b4204ad..78d1814d2c1e 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -2857,10 +2857,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))
