https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124545
--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Philipp Tomsich <[email protected]>: https://gcc.gnu.org/g:8395fa7c79eecf102b276ffee37436e75a522978 commit r17-2078-g8395fa7c79eecf102b276ffee37436e75a522978 Author: Philipp Tomsich <[email protected]> Date: Wed Jul 1 12:19:30 2026 +0200 tree-optimization/124545 - VN: add inverse widening lookup for PLUS/MINUS visit_nary_op canonicalises (T)(A + C) into (T)A + (T)C for its VN lookup, but not the reverse -- so whether VN discovers (T)A + C == (T)(A + C) depends on which form it sees first. Add a match.pd rule that rewrites (T)A +- CST into (T)(A +- CST') using the op! qualifier, so the fold only fires when the narrow expression already has a value number -- i.e. only inside VN via mprts_hook. Restrict to TYPE_OVERFLOW_UNDEFINED inner types: for unsigned inner the narrow op wraps mod 2^prec (defined) while the widened outer op does not, changing the observed value (bitfld-5.c is the concrete miscompile when the guard is loosened). Use wi::min_precision (CST, SIGNED) rather than int_fits_type_p for the fits-check, so sign-encoded small negatives (e.g. -1 as sizetype's 0xFFFF...FFFF) qualify. PR tree-optimization/124545 gcc/ChangeLog: * match.pd: Add (T)A +- CST -> (T)(A +- CST') for widening conversions from a signed inner type with undefined overflow. gcc/testsuite/ChangeLog: * gcc.dg/pr124545.c: New test. * gcc.dg/pr124545-2.c: New test.
