[php-src] PR #25606: Fix optimizer turning $i++ < $n into ++$i <= $n for non-integer operands

From: Date: Thu, 19 Nov 2026 18:17:52 +0000
Subject: [php-src] PR #25606: Fix optimizer turning $i++ < $n into ++$i <= $n for non-integer operands
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-40827@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25606 Author: ChrisAbner The DFA pass rewrites $i++ < $n as ++$i <= $n, and $n < $i-- as $n <= --$i. It only checked that the result of the ++/-- is int or float. It did not look at the other operand of <. The two forms are only equivalent when both sides are integers and the increment does not overflow. With OPcache, without the JIT and at the default optimization level, this goes wrong in two ways. A loop with a non-integer bound runs one iteration less. function half(array $a) { $n = count($a) / 2; $i = 0; $o = []; while ($i++ < $n) { $o[] = $a[$i - 1]; } return $o; } returns [1,2,3] for [1,2,3,4,5] without OPcache and [1,2] with it. The same happens with a numeric string, null or bool bound, with a float counter, and with $n < $i-- and a float $n. And on 64-bit, while ($i++ < PHP_INT_MAX) never ends. ++$i overflows to the float 2^63, and 2^63 <= PHP_INT_MAX is true when compared as doubles. The fix only rewrites when the counter, the incremented value and the other operand of < can only be int. Type inference already gives the incremented value int|float when it may overflow (the /* may overflow */ case of POST_INC/POST_DEC in zend_inference.c), so checking it rules out the overflow. The other operand is read with _ssa_op2_info()/_ssa_op1_info(), which also covers constants. Checking only the bound was the other option. It fixes the fractional cases but keeps the endless loop. The cost is that $i++ < $n with an int $n of unknown range is no longer rewritten. The rewrite only saves a copy to a temporary. In a release build, for ($i = 0; $i++ < $n;) with an int $n over 30M iterations took 0.086 s before and 0.085 s after without the JIT (median of 7 runs). A literal bound like $i++ < 3 is still rewritten. This follows #25074, which also restricted a rewrite in the same function to the types where it is equivalent. Introduced by de78786566 ("Optimize out result value of ASSIGN, ASSIGN_OP and INC/DEC opcodes, if possible"), first released in 8.0.1. Confirmed by bisect: its parent c2fbab392c gets all the cases below right, de78786566 gets 9 of them wrong. Reproduced on the official images of 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.28, 8.5.13 and 8.6.0. The patch applies cleanly on PHP-8.5, PHP-8.6 and master. **Verification** New test ext/opcache/tests/opt/inc_dec_is_smaller.phpt. Results with OPcache and without the JIT: | Case | Without OPcache | Before | After | |---|---|---|---| | half([1, 2, 3, 4, 5]) | [1,2,3] | [1,2] | [1,2,3] | | float bound 2.5 | 3 iterations | 2 | 3 | | numeric string bound "2.5" | 3 | 2 | 3 | | float counter, $x = 0.5; while ($x++ < 3) | 3 | 2 | 3 | | $min < $i-- with $min = 0.5 | 3 | 2 | 3 | | $i++ < $n with $i = -1, $n = null | false | true | false | | $i++ < $n with $i = -1, $n = false | false | true | false | | PHP_INT_MAX - 1 up to PHP_INT_MAX (capped at 5) | 1 | 5 | 1 | | PHP_INT_MIN + 1 down to PHP_INT_MIN (capped at 5) | 1 | 5 | 1 | | int bound, literal bound, int $min with $i-- | 3 | 3 | 3 | The test fails without the fix and passes with it, without the JIT, with opcache.jit=tracing and with opcache.jit=function. It also passes with opcache.file_cache_only and on a 32-bit build. On 32-bit the overflow cases already pass on the base, because PHP_INT_MAX fits exactly in a double there. The other cases fail on the base. On PHP-8.4 (ab05b44386), Zend/tests runs with 0 failures without OPcache (4953 passed) and with it (4957 passed). ext/opcache/tests has 2 failures without the JIT (gh8739, gh17733) and 4 with tracing (the same two plus preload_restart_hot_counters and preload_restart_function_jit). They start a sub-process or restart OPcache, and they fail the same way on the base commit in my environment. On master (fc4f12281b), Zend/tests and ext/opcache/tests run with 0 failures, with OPcache and with tracing. Not covered: $i++ < 3.0 is no longer rewritten. It was correct, but a float literal bound is rare. block_pass.c turns !(a < b) into b <= a, which is the known NaN case (GH-15773), so I left it out.

« previous php.git-pulls (#40827) next »