https://gcc.gnu.org/g:6b8c4689d99d36dc4e849a97bcf14b200d5d47cb
commit r17-6637-g6b8c4689d99d36dc4e849a97bcf14b200d5d47cb Author: Richard Biener <[email protected]> Date: Tue Nov 24 16:26:42 2026 +0100 tree-optimization/128482 - more UB with SCEV The following fixes more of the overflow cases accumulating CHREC_RIGHT by moving the PR125652/PR121370 fixes to a generic place where it also catches the CST - CHREC handling in chrec_fold_plus_1. PR tree-optimization/128482 PR tree-optimization/125652 PR tree-optimization/121370 * tree-chrec.h (build_polynomial_chrec): Return chrec_dont_know when CHREC_RIGHT overflowed. Moved from ... * tree-chrec.cc (chrec_fold_plus_poly_poly): ... here. * tree-scalar-evolution.cc (scev_dfs::add_to_evolution_1): ... and here. * gcc.dg/torture/pr128482-1.c: New testcase. * gcc.dg/torture/pr128482-2.c: Likewise. * gcc.dg/torture/pr128482-3.c: Likewise. Diff: --- gcc/testsuite/gcc.dg/torture/pr128482-1.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr128482-2.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr128482-3.c | 21 +++++++++++++++++++++ gcc/tree-chrec.cc | 10 ---------- gcc/tree-chrec.h | 13 +++++++++++++ gcc/tree-scalar-evolution.cc | 11 ----------- 6 files changed, 76 insertions(+), 21 deletions(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr128482-1.c b/gcc/testsuite/gcc.dg/torture/pr128482-1.c new file mode 100644 index 000000000000..3dab2290b7f9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128482-1.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +__attribute__((noipa)) int f (int n) +{ + int b = 5, d = 0, k = 0; + for (;;) + { + d = -10 - b; + if (++k >= n) + break; + b += -2147483647 - 1; + } + return d; +} +int main (void) +{ + if (f (2) != 2147483633) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr128482-2.c b/gcc/testsuite/gcc.dg/torture/pr128482-2.c new file mode 100644 index 000000000000..0b6d234d2bc6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128482-2.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +__attribute__((noipa)) int f (int n) +{ + int b = 20, d = 0, k = 0; + for (;;) + { + d = ~b; + if (++k >= n) + break; + b += -2147483647 - 1; + } + return d; +} +int main (void) +{ + if (f (2) != 2147483627) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr128482-3.c b/gcc/testsuite/gcc.dg/torture/pr128482-3.c new file mode 100644 index 000000000000..75df25281ed9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128482-3.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +__attribute__((noipa)) int f (int n) +{ + int b = 13, d = 0, k = 0; +top: + d = -414612191 - b * 2; + if (++k < n) + { + b += -1073741824; + goto top; + } + return d; +} +int main (void) +{ + if (f (2) != 1732871431) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc index d1845ac69778..a6768f4593a2 100644 --- a/gcc/tree-chrec.cc +++ b/gcc/tree-chrec.cc @@ -127,16 +127,6 @@ chrec_fold_plus_poly_poly (enum tree_code code, if (chrec_zerop (right)) return left; - /* When we have an evolution in a non-wrapping type and in the process of - accumulating CHREC_RIGHT there was overflow this indicates in the - association that happened in accumulating the CHRECs clearly involved UB. - Avoid this. When accumulating two CHRECs we basically turn - (a + INCR1) + INCR2 into a + (INCR1 + INCR2) which is not always valid. - Note this check only catches few invalid cases. */ - else if ((INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type)) - && TREE_CODE (right) == INTEGER_CST - && TREE_OVERFLOW (right)) - return chrec_dont_know; else return build_polynomial_chrec (CHREC_VARIABLE (poly0), left, right); diff --git a/gcc/tree-chrec.h b/gcc/tree-chrec.h index 9fdfaac0e150..b965ae893ff0 100644 --- a/gcc/tree-chrec.h +++ b/gcc/tree-chrec.h @@ -161,6 +161,19 @@ build_polynomial_chrec (unsigned loop_num, if (chrec_zerop (right)) return left; + /* When we have an evolution in a non-wrapping type and in the + process of accumulating CHREC_RIGHT there was an overflow this + indicates in the association that happened in accumulating the + CHRECs clearly involved UB. Avoid this. When accumulating two + CHRECs we basically turn (a + INCR1) + INCR2 into a + (INCR1 + INCR2) + which is not always valid. Note this check only catches few + invalid cases. */ + if (INTEGRAL_TYPE_P (TREE_TYPE (right)) + && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (right)) + && TREE_CODE (right) == INTEGER_CST + && TREE_OVERFLOW (right)) + return chrec_dont_know; + tree chrec = build2 (POLYNOMIAL_CHREC, TREE_TYPE (left), left, right); CHREC_VARIABLE (chrec) = loop_num; return chrec; diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc index 2503c96ed0ab..37e2c708e2fe 100644 --- a/gcc/tree-scalar-evolution.cc +++ b/gcc/tree-scalar-evolution.cc @@ -719,17 +719,6 @@ scev_dfs::add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt) to_add = chrec_convert (type, to_add, at_stmt); right = chrec_convert_rhs (type, right, at_stmt); right = chrec_fold_plus (chrec_type (right), right, to_add); - /* When we have an evolution in a non-wrapping type and - in the process of accumulating CHREC_RIGHT there was - overflow this indicates in the association that happened - in building the CHREC clearly involved UB. Avoid this. - In building a CHREC we basically turn (a + INCR1) + INCR2 - into a + (INCR1 + INCR2) which is not always valid. - Note this check only catches few invalid cases. */ - if ((INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type)) - && TREE_CODE (right) == INTEGER_CST - && TREE_OVERFLOW (right)) - return chrec_dont_know; return build_polynomial_chrec (var, left, right); } else
