The following testcase ICEs, because I misremembered what the return value
from match_arith_overflow is. It isn't true if __builtin_*_overflow was
matched, but it is true only in the BIT_NOT_EXPR case if stmt was removed.
So, if match_arith_overflow matches something, gsi_stmt (gsi) will not
be stmt and match_uaddc_usubc will be confused and can ICE.
The following patch fixes it by checking if gsi_stmt (gsi) == stmt,
in that case we know it is still a PLUS_EXPR/MINUS_EXPR and we can try to
pattern match it further as UADDC/USUBC.
2023-06-16 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/110271
* tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children)
<case PLUS_EXPR>: Ignore return value from match_arith_overflow,
instead call match_uaddc_usubc only if gsi_stmt (gsi) is still stmt.
* gcc.c-torture/compile/pr110271.c: New test.
--- /dev/null
+/* PR tree-optimization/110271 */
+
+unsigned a, b, c, d, e;
+
+void
+foo (unsigned *x, int y, unsigned int *z)
+{
+ for (int i = 0; i < y; i++)
+ {
+ b += d;
+ a += b < d;
+ a += c = (__PTRDIFF_TYPE__) x > 3;
+ d = z[1] + (a < c);
+ a += e;
+ d += a < e;
+ }
+}
+
+void
+bar (unsigned int *z)
+{
+ unsigned *x = x;
+ foo (x, 9, z);
+}
case PLUS_EXPR:
case MINUS_EXPR:
- if (!convert_plusminus_to_widen (&gsi, stmt, code)
- && !match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p))
- match_uaddc_usubc (&gsi, stmt, code);
+ if (!convert_plusminus_to_widen (&gsi, stmt, code))
+ {
+ match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p);
+ if (gsi_stmt (gsi) == stmt)
+ match_uaddc_usubc (&gsi, stmt, code);
+ }
break;
case BIT_NOT_EXPR: