Commit fe33e5801e5 for php

commit fe33e5801e5c94fb3cd5f37249ee9bfc9d8e8e43
Author: ChrisAbner <chrisabner@hotmail.com>
Date:   Wed Nov 18 17:25:31 2026 -0500

    Zend/Optimizer: Keep float + 0.0 that can turn -0.0 into 0.0

    The DFA pass replaced x + 0.0, 0.0 + x and x - -0.0 with a copy of x when x
    is known to be a double, but -0.0 + 0.0 is 0.0, so the copy kept -0.0. The
    long 0 in $x + 0 is turned into 0.0 first, so that form was affected too.
    Only rewrite a double ADD when the constant is -0.0, and a double SUB when
    it is 0.0, comparing bitwise.

    Fixes GH-25744
    Closes GH-25608

diff --git a/NEWS b/NEWS
index 6bcd9a76ed1..cb15756360e 100644
--- a/NEWS
+++ b/NEWS
@@ -195,6 +195,8 @@ PHP                                                                        NEWS
   . Fixed bug GH-24319 (zend_accel_hash_update() links new entries without a
     release barrier). (Levi Morrison)
   . Fixed JIT leaving state after killed processes. (dunglas)
+  . Fixed bug GH-25744 (Optimizer folds $float + 0.0 to a copy, giving wrong
+    result for -0.0). (ChrisAbner)

 - PDO_DBLIB:
   . Fixed a crash on persistent connections after an error raised through
diff --git a/Zend/Optimizer/dfa_pass.c b/Zend/Optimizer/dfa_pass.c
index 6dd35cb9f37..2ad444a4a91 100644
--- a/Zend/Optimizer/dfa_pass.c
+++ b/Zend/Optimizer/dfa_pass.c
@@ -1034,6 +1034,10 @@ static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ss
 	return 0;
 }

+/* Compared bitwise with memcmp(), so that 0.0 and -0.0 are told apart */
+static const double neg_zero = -0.0;
+static const double pos_zero = 0.0;
+
 void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map)
 {
 	if (ctx->debug_level & ZEND_DUMP_BEFORE_DFA_PASS) {
@@ -1160,9 +1164,11 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
 							  && Z_LVAL_P(zv) == 0)
 							 || ((OP2_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE
 							  && Z_TYPE_INFO_P(zv) == IS_DOUBLE
-							  && Z_DVAL_P(zv) == 0.0)) {
+							  /* 0.0 + -0.0 is 0.0, so only -0.0 is the identity */
+							  && memcmp(&Z_DVAL_P(zv), &neg_zero, sizeof(double)) == 0)) {

-// op_1: #v.? = ADD 0, #?.? [double,long] => #v.? = QM_ASSIGN #?.?
+// op_1: #v.? = ADD 0, #?.? [long] => #v.? = QM_ASSIGN #?.?
+// op_1: #v.? = ADD -0.0, #?.? [double] => #v.? = QM_ASSIGN #?.?

 								opline->opcode = ZEND_QM_ASSIGN;
 								opline->op1_type = opline->op2_type;
@@ -1211,9 +1217,12 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
 							  && Z_LVAL_P(zv) == 0)
 							 || ((OP1_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE
 							  && Z_TYPE_INFO_P(zv) == IS_DOUBLE
-							  && Z_DVAL_P(zv) == 0.0)) {
+							  /* -0.0 + 0.0 and -0.0 - -0.0 are 0.0, so only x + -0.0 and x - 0.0 are x */
+							  && memcmp(&Z_DVAL_P(zv), opline->opcode == ZEND_ADD ? &neg_zero : &pos_zero, sizeof(double)) == 0)) {

-// op_1: #v.? = ADD #?.? [double,long], 0 => #v.? = QM_ASSIGN #?.?
+// op_1: #v.? = ADD #?.? [long], 0 => #v.? = QM_ASSIGN #?.?
+// op_1: #v.? = ADD #?.? [double], -0.0 => #v.? = QM_ASSIGN #?.?
+// op_1: #v.? = SUB #?.? [double], 0.0 => #v.? = QM_ASSIGN #?.?

 								opline->opcode = ZEND_QM_ASSIGN;
 								opline->op2_type = IS_UNUSED;
diff --git a/ext/opcache/tests/opt/add_sub_zero_signed_zero.phpt b/ext/opcache/tests/opt/add_sub_zero_signed_zero.phpt
new file mode 100644
index 00000000000..a2462c7040b
--- /dev/null
+++ b/ext/opcache/tests/opt/add_sub_zero_signed_zero.phpt
@@ -0,0 +1,76 @@
+--TEST--
+DFA pass must not drop a float + 0.0 that turns -0.0 into 0.0
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+--FILE--
+<?php
+
+function add_int_zero(float $x) { return $x + 0; }
+function add_zero_left(float $x) { return 0 + $x; }
+function add_float_zero(float $x) { return $x + 0.0; }
+function add_float_zero_left(float $x) { return 0.0 + $x; }
+function sub_neg_zero(float $x) { return $x - -0.0; }
+function round_add_zero($x) { return round($x, 2) + 0.0; }
+function cast_add_zero($x) { return 0 + (float) $x; }
+
+class C { public float $p = -0.0; }
+function prop_add_zero(C $c) { return $c->p + 0; }
+
+/* These were already correct and must stay so */
+function sub_zero(float $x) { return $x - 0.0; }
+function sub_int_zero(float $x) { return $x - 0; }
+function add_neg_zero(float $x) { return $x + -0.0; }
+function add_neg_zero_left(float $x) { return -0.0 + $x; }
+function int_add_zero(int $x) { return $x + 0; }
+
+foreach ([
+    'add_int_zero', 'add_zero_left', 'add_float_zero', 'add_float_zero_left', 'sub_neg_zero',
+    'sub_zero', 'sub_int_zero', 'add_neg_zero', 'add_neg_zero_left',
+] as $f) {
+    echo $f, ': ';
+    var_dump($f(-0.0), $f(0.0), $f(1.5));
+}
+echo "round_add_zero: "; var_dump(round_add_zero(-0.004));
+echo "cast_add_zero: "; var_dump(cast_add_zero("-0"));
+echo "prop_add_zero: "; var_dump(prop_add_zero(new C));
+echo "int_add_zero: "; var_dump(int_add_zero(-3));
+echo json_encode(round_add_zero(-0.004)), "\n";
+
+?>
+--EXPECT--
+add_int_zero: float(0)
+float(0)
+float(1.5)
+add_zero_left: float(0)
+float(0)
+float(1.5)
+add_float_zero: float(0)
+float(0)
+float(1.5)
+add_float_zero_left: float(0)
+float(0)
+float(1.5)
+sub_neg_zero: float(0)
+float(0)
+float(1.5)
+sub_zero: float(-0)
+float(0)
+float(1.5)
+sub_int_zero: float(-0)
+float(0)
+float(1.5)
+add_neg_zero: float(-0)
+float(0)
+float(1.5)
+add_neg_zero_left: float(-0)
+float(0)
+float(1.5)
+round_add_zero: float(0)
+cast_add_zero: float(0)
+prop_add_zero: float(0)
+int_add_zero: int(-3)
+0
diff --git a/ext/opcache/tests/opt/add_sub_zero_signed_zero_opt.phpt b/ext/opcache/tests/opt/add_sub_zero_signed_zero_opt.phpt
new file mode 100644
index 00000000000..6f6c8bb8ff8
--- /dev/null
+++ b/ext/opcache/tests/opt/add_sub_zero_signed_zero_opt.phpt
@@ -0,0 +1,114 @@
+--TEST--
+DFA pass must keep a float + 0.0 that can turn -0.0 into 0.0, and still drop the real identities
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.opt_debug_level=0x20000
+opcache.preload=
+zend_test.observer.enabled=0
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+/* x + 0.0 can turn -0.0 into 0.0, so these keep the ADD/SUB */
+function add_int_zero(float $x) { return $x + 0; }
+function add_zero_left(float $x) { return 0 + $x; }
+function add_float_zero(float $x) { return $x + 0.0; }
+function add_float_zero_left(float $x) { return 0.0 + $x; }
+function sub_neg_zero(float $x) { return $x - -0.0; }
+
+/* These give back x for every float and still become a copy */
+function sub_zero(float $x) { return $x - 0.0; }
+function sub_int_zero(float $x) { return $x - 0; }
+function add_neg_zero(float $x) { return $x + -0.0; }
+function add_neg_zero_left(float $x) { return -0.0 + $x; }
+function int_add_zero(int $x) { return $x + 0; }
+?>
+--EXPECTF--
+
+$_main:
+     ; (lines=1, args=0, vars=0, tmps=0)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:1-16
+0000 RETURN int(1)
+
+add_int_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:3-3
+0000 CV0($x) = RECV 1
+0001 T1 = ADD CV0($x) float(0)
+0002 RETURN T1
+
+add_zero_left:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:4-4
+0000 CV0($x) = RECV 1
+0001 T1 = ADD CV0($x) float(0)
+0002 RETURN T1
+
+add_float_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:5-5
+0000 CV0($x) = RECV 1
+0001 T1 = ADD CV0($x) float(0)
+0002 RETURN T1
+
+add_float_zero_left:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:6-6
+0000 CV0($x) = RECV 1
+0001 T1 = ADD CV0($x) float(0)
+0002 RETURN T1
+
+sub_neg_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:7-7
+0000 CV0($x) = RECV 1
+0001 T1 = SUB CV0($x) float(-0)
+0002 RETURN T1
+
+sub_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:10-10
+0000 CV0($x) = RECV 1
+0001 T1 = QM_ASSIGN CV0($x)
+0002 RETURN T1
+
+sub_int_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:11-11
+0000 CV0($x) = RECV 1
+0001 T1 = QM_ASSIGN CV0($x)
+0002 RETURN T1
+
+add_neg_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:12-12
+0000 CV0($x) = RECV 1
+0001 T1 = QM_ASSIGN CV0($x)
+0002 RETURN T1
+
+add_neg_zero_left:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:13-13
+0000 CV0($x) = RECV 1
+0001 T1 = QM_ASSIGN CV0($x)
+0002 RETURN T1
+
+int_add_zero:
+     ; (lines=3, args=1, vars=1, tmps=1)
+     ; (after optimizer)
+     ; %sadd_sub_zero_signed_zero_opt.php:14-14
+0000 CV0($x) = RECV 1
+0001 T1 = QM_ASSIGN CV0($x)
+0002 RETURN T1