https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929
--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The releases/gcc-14 branch has been updated by Gaius Mulley <ga...@gcc.gnu.org>: https://gcc.gnu.org/g:d811080341adf9d805e3f79a8fd9be2e13bd9848 commit r14-10166-gd811080341adf9d805e3f79a8fd9be2e13bd9848 Author: Gaius Mulley <gaiusm...@gmail.com> Date: Fri May 3 22:58:11 2024 +0100 [PATCH] PR modula2/114929 for loop fails to iterate down to zero There is a bug in the for loop control code which is exposed when an unsigned type is used in the iterator variable. See gm2/pim/run/pass/testforloopzero[234].mod. The bug is in the calculation of the last iterator value. The bug fix is to avoid using negative expressions when calculating the last iterator value with a negative step value. This patch detects if e1, e2, step value are all constant, in which case the ztype is used internally and there is no overflow. If the last iterator value is held in a variable then it uses a different method to calculate the last iterator depending upon the sign of the step value. gcc/m2/ChangeLog: PR modula2/114929 * gm2-compiler/M2Quads.mod (ForLoopLastIteratorVariable): New procedure. (ForLoopLastIteratorConstant): Ditto. (ForLoopLastIterator): Ditto. (BuildForToByDo): Remove LastIterator calculation and call ForLoopLastIterator instead. (FinalValue): Replace with ... (LastIterator): ... this. gcc/testsuite/ChangeLog: PR modula2/114929 * gm2/pim/run/pass/testforloopzero.mod: New test. * gm2/pim/run/pass/testforloopzero2.mod: New test. * gm2/pim/run/pass/testforloopzero3.mod: New test. * gm2/pim/run/pass/testforloopzero4.mod: New test. (cherry picked from commit a561dc0f6c7085e102fe9e9b6abd7f2138512576) Signed-off-by: Gaius Mulley <gaiusm...@gmail.com>