[php-src] master: Improve optimizer support for class constants (#13438)
| From: | Máté Kocsis via GitHub | Date: | Mon, 18 Mar 2024 06:44:32 +0000 |
| Subject: | [php-src] master: Improve optimizer support for class constants (#13438) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127769@lists.php.net to get a copy of this message | ||
Author: Máté Kocsis (kocsismate)
Committer: GitHub (web-flow)
Pusher: kocsismate
Date: 2024-03-18T07:30:44+01:00
Commit: https://github.com/php/php-src/commit/56cf09f23a66820640562733b4d28c1f51b2b450
Raw diff: https://github.com/php/php-src/commit/56cf09f23a66820640562733b4d28c1f51b2b450.diff
Improve optimizer support for class constants (#13438)
The following optimizations are added:
- Constant folding of final class constants
- Type inference of typed class constants
Changed paths:
A ext/opcache/tests/opt/type_inference_class_consts1.phpt
A ext/opcache/tests/opt/type_inference_class_consts2.phpt
A ext/opcache/tests/opt/type_inference_class_consts3.phpt
A ext/opcache/tests/opt/type_inference_class_consts4.phpt
M Zend/Optimizer/pass1.c
M Zend/Optimizer/zend_inference.c
M Zend/Optimizer/zend_optimizer.c
M Zend/Optimizer/zend_optimizer_internal.h
Diff:
diff --git a/Zend/Optimizer/pass1.c b/Zend/Optimizer/pass1.c
index 60094eee4f60b..ba2bf188102c2 100644
--- a/Zend/Optimizer/pass1.c
+++ b/Zend/Optimizer/pass1.c
@@ -155,33 +155,26 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
}
break;
- case ZEND_FETCH_CLASS_CONSTANT:
- if (opline->op2_type == IS_CONST &&
- Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) {
-
- zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
- ctx->script, op_array, opline);
- if (ce) {
- zend_class_constant *cc = zend_hash_find_ptr(
- &ce->constants_table, Z_STR(ZEND_OP2_LITERAL(opline)));
- if (cc && !(ZEND_CLASS_CONST_FLAGS(cc) & ZEND_ACC_DEPRECATED) &&
(ZEND_CLASS_CONST_FLAGS(cc) & ZEND_ACC_PPP_MASK) == ZEND_ACC_PUBLIC && !(ce->ce_flags
& ZEND_ACC_TRAIT)) {
- zval *c = &cc->value;
- if (Z_TYPE_P(c) == IS_CONSTANT_AST) {
- zend_ast *ast = Z_ASTVAL_P(c);
- if (ast->kind != ZEND_AST_CONSTANT
- || !zend_optimizer_get_persistent_constant(zend_ast_get_constant_name(ast), &result, 1)
- || Z_TYPE(result) == IS_CONSTANT_AST) {
- break;
- }
- } else {
- ZVAL_COPY_OR_DUP(&result, c);
- }
-
- replace_by_const_or_qm_assign(op_array, opline, &result);
- }
+ case ZEND_FETCH_CLASS_CONSTANT: {
+ bool is_prototype;
+ const zend_class_constant *cc = zend_fetch_class_const_info(ctx->script, op_array, opline,
&is_prototype);
+ if (!cc || is_prototype) {
+ break;
+ }
+ const zval *c = &cc->value;
+ if (Z_TYPE_P(c) == IS_CONSTANT_AST) {
+ zend_ast *ast = Z_ASTVAL_P(c);
+ if (ast->kind != ZEND_AST_CONSTANT
+ || !zend_optimizer_get_persistent_constant(zend_ast_get_constant_name(ast), &result, 1)
+ || Z_TYPE(result) == IS_CONSTANT_AST) {
+ break;
}
+ } else {
+ ZVAL_COPY_OR_DUP(&result, c);
}
+ replace_by_const_or_qm_assign(op_array, opline, &result);
break;
+ }
case ZEND_DO_ICALL: {
zend_op *send1_opline = opline - 1;
diff --git a/Zend/Optimizer/zend_inference.c b/Zend/Optimizer/zend_inference.c
index 4b4bbb09d8105..a4aca39e2edbc 100644
--- a/Zend/Optimizer/zend_inference.c
+++ b/Zend/Optimizer/zend_inference.c
@@ -3911,9 +3911,21 @@ static zend_always_inline zend_result _zend_update_type_info(
UPDATE_SSA_OBJ_TYPE(zend_ce_closure, /* is_instanceof */ false, ssa_op->result_def);
break;
case ZEND_FETCH_CONSTANT:
- case ZEND_FETCH_CLASS_CONSTANT:
UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY,
ssa_op->result_def);
break;
+ case ZEND_FETCH_CLASS_CONSTANT: {
+ bool is_prototype;
+ const zend_class_constant *cc = zend_fetch_class_const_info(script, op_array, opline,
&is_prototype);
+ if (!cc || !ZEND_TYPE_IS_SET(cc->type)) {
+ UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY,
ssa_op->result_def);
+ break;
+ }
+ UPDATE_SSA_TYPE(zend_convert_type(script, cc->type, &ce), ssa_op->result_def);
+ if (ce) {
+ UPDATE_SSA_OBJ_TYPE(ce, /* is_instanceof */ true, ssa_op->result_def);
+ }
+ break;
+ }
case ZEND_STRLEN:
case ZEND_COUNT:
case ZEND_FUNC_NUM_ARGS:
diff --git a/Zend/Optimizer/zend_optimizer.c b/Zend/Optimizer/zend_optimizer.c
index 5816584edb173..3738e058cd881 100644
--- a/Zend/Optimizer/zend_optimizer.c
+++ b/Zend/Optimizer/zend_optimizer.c
@@ -831,6 +831,64 @@ zend_class_entry *zend_optimizer_get_class_entry_from_op1(
return NULL;
}
+const zend_class_constant *zend_fetch_class_const_info(
+ const zend_script *script, const zend_op_array *op_array, const zend_op *opline, bool
*is_prototype) {
+ const zend_class_entry *ce = NULL;
+ bool is_static_reference = false;
+
+ if (!opline || !op_array || opline->op2_type != IS_CONST ||
Z_TYPE_P(CRT_CONSTANT(opline->op2)) != IS_STRING) {
+ return NULL;
+ }
+ if (opline->op1_type == IS_CONST) {
+ zval *op1 = CRT_CONSTANT(opline->op1);
+ if (Z_TYPE_P(op1) == IS_STRING) {
+ if (script) {
+ ce = zend_optimizer_get_class_entry(script, op_array, Z_STR_P(op1 + 1));
+ } else {
+ zend_class_entry *tmp = zend_hash_find_ptr(EG(class_table), Z_STR_P(op1 + 1));
+ if (tmp != NULL) {
+ if (tmp->type == ZEND_INTERNAL_CLASS) {
+ ce = tmp;
+ } else if (tmp->type == ZEND_USER_CLASS
+ && tmp->info.user.filename
+ && tmp->info.user.filename == op_array->filename) {
+ ce = tmp;
+ }
+ }
+ }
+ }
+ } else if (opline->op1_type == IS_UNUSED
+ && op_array->scope && !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)
+ && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
+ int fetch_type = opline->op1.num & ZEND_FETCH_CLASS_MASK;
+ if (fetch_type == ZEND_FETCH_CLASS_SELF) {
+ ce = op_array->scope;
+ } else if (fetch_type == ZEND_FETCH_CLASS_STATIC) {
+ ce = op_array->scope;
+ is_static_reference = true;
+ } else if (fetch_type == ZEND_FETCH_CLASS_PARENT) {
+ if (op_array->scope->ce_flags & ZEND_ACC_LINKED) {
+ ce = op_array->scope->parent;
+ }
+ }
+ }
+ if (!ce || (ce->ce_flags & ZEND_ACC_TRAIT)) {
+ return NULL;
+ }
+ zend_class_constant *const_info = zend_hash_find_ptr(&ce->constants_table,
Z_STR_P(CRT_CONSTANT(opline->op2)));
+ if (!const_info) {
+ return NULL;
+ }
+ if ((ZEND_CLASS_CONST_FLAGS(const_info) & ZEND_ACC_DEPRECATED)
+ || ((ZEND_CLASS_CONST_FLAGS(const_info) & ZEND_ACC_PPP_MASK) != ZEND_ACC_PUBLIC &&
const_info->ce != op_array->scope)) {
+ return NULL;
+ }
+ *is_prototype = is_static_reference
+ && !(const_info->ce->ce_flags & ZEND_ACC_FINAL) &&
!(ZEND_CLASS_CONST_FLAGS(const_info) & ZEND_ACC_FINAL);
+
+ return const_info;
+}
+
zend_function *zend_optimizer_get_called_func(
zend_script *script, zend_op_array *op_array, zend_op *opline, bool *is_prototype)
{
diff --git a/Zend/Optimizer/zend_optimizer_internal.h b/Zend/Optimizer/zend_optimizer_internal.h
index 2f708471ab1d5..a1fc57092d179 100644
--- a/Zend/Optimizer/zend_optimizer_internal.h
+++ b/Zend/Optimizer/zend_optimizer_internal.h
@@ -105,6 +105,8 @@ zend_class_entry *zend_optimizer_get_class_entry(
const zend_script *script, const zend_op_array *op_array, zend_string *lcname);
zend_class_entry *zend_optimizer_get_class_entry_from_op1(
const zend_script *script, const zend_op_array *op_array, const zend_op *opline);
+const zend_class_constant *zend_fetch_class_const_info(
+ const zend_script *script, const zend_op_array *op_array, const zend_op *opline, bool
*is_prototype);
void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx);
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx);
diff --git a/ext/opcache/tests/opt/type_inference_class_consts1.phpt
b/ext/opcache/tests/opt/type_inference_class_consts1.phpt
new file mode 100644
index 0000000000000..5d53f5e629c64
--- /dev/null
+++ b/ext/opcache/tests/opt/type_inference_class_consts1.phpt
@@ -0,0 +1,91 @@
+--TEST--
+Test type inference of class consts - reference by static and self
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.opt_debug_level=0x20000
+opcache.preload=
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+
+class Test1 {
+ public const FOO = 42;
+ public final const BAR = 42;
+ public const int BAZ = 42;
+
+ public function getSelfFoo(): int {
+ return self::FOO;
+ }
+
+ public function getSelfBar(): int {
+ return self::BAR;
+ }
+
+ public function getSelfBaz(): int {
+ return self::BAZ;
+ }
+
+ public function getStaticFoo(): int {
+ return static::FOO;
+ }
+
+ public function getStaticBar(): int {
+ return static::BAR;
+ }
+
+ public function getStaticBaz(): int {
+ return static::BAZ;
+ }
+}
+
+?>
+--EXPECTF--
+$_main:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(1)
+
+Test1::getSelfFoo:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test1::getSelfBar:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test1::getSelfBaz:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test1::getStaticFoo:
+ ; (lines=3, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (static) (exception) string("FOO")
+0001 VERIFY_RETURN_TYPE T0
+0002 RETURN T0
+LIVE RANGES:
+ 0: 0001 - 0002 (tmp/var)
+
+Test1::getStaticBar:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test1::getStaticBaz:
+ ; (lines=2, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (static) (exception) string("BAZ")
+0001 RETURN T0
diff --git a/ext/opcache/tests/opt/type_inference_class_consts2.phpt
b/ext/opcache/tests/opt/type_inference_class_consts2.phpt
new file mode 100644
index 0000000000000..b2ffd6f4df4e3
--- /dev/null
+++ b/ext/opcache/tests/opt/type_inference_class_consts2.phpt
@@ -0,0 +1,58 @@
+--TEST--
+Test type inference of class consts - reference by parent
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.opt_debug_level=0x20000
+opcache.preload=
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+
+class Test1 {
+ public const FOO = 42;
+ public final const BAR = 42;
+ public const int BAZ = 42;
+}
+
+final class Test2 extends Test1 {
+ public function getParentFoo(): int {
+ return parent::FOO;
+ }
+
+ public function getParentBar(): int {
+ return parent::BAR;
+ }
+
+ public function getParentBaz(): int {
+ return parent::BAZ;
+ }
+}
+
+?>
+--EXPECTF--
+$_main:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(1)
+
+Test2::getParentFoo:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test2::getParentBar:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test2::getParentBaz:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
diff --git a/ext/opcache/tests/opt/type_inference_class_consts3.phpt
b/ext/opcache/tests/opt/type_inference_class_consts3.phpt
new file mode 100644
index 0000000000000..f276676ba69a3
--- /dev/null
+++ b/ext/opcache/tests/opt/type_inference_class_consts3.phpt
@@ -0,0 +1,58 @@
+--TEST--
+Test type inference of class consts - reference by class name
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.opt_debug_level=0x20000
+opcache.preload=
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+
+class Test1 {
+ public const FOO = 42;
+ public final const BAR = 42;
+ public const int BAZ = 42;
+}
+
+class Test3 {
+ public function getTestFoo(): int {
+ return Test1::FOO;
+ }
+
+ public function getTestBar(): int {
+ return Test1::BAR;
+ }
+
+ public function getTestBaz(): int {
+ return Test1::BAZ;
+ }
+}
+
+?>
+--EXPECTF--
+$_main:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(1)
+
+Test3::getTestFoo:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test3::getTestBar:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test3::getTestBaz:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
diff --git a/ext/opcache/tests/opt/type_inference_class_consts4.phpt
b/ext/opcache/tests/opt/type_inference_class_consts4.phpt
new file mode 100644
index 0000000000000..d1995575d64ef
--- /dev/null
+++ b/ext/opcache/tests/opt/type_inference_class_consts4.phpt
@@ -0,0 +1,104 @@
+--TEST--
+Test type inference of class consts - interface consts
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.opt_debug_level=0x20000
+opcache.preload=
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+
+interface TestI {
+ public const int A = 42;
+ public final const B = 42;
+}
+
+class Test4 implements TestI {
+ public function getSelfA(): int {
+ return self::A;
+ }
+
+ public function getSelfB(): int {
+ return self::B;
+ }
+
+ public function getStaticA(): int {
+ return static::A;
+ }
+
+ public function getStaticB(): int {
+ return static::B;
+ }
+
+ public function getTestIA(): int {
+ return TestI::A;
+ }
+
+ public function getTestIB(): int {
+ return TestI::B;
+ }
+}
+
+?>
+--EXPECTF--
+$_main:
+ ; (lines=2, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 DECLARE_CLASS string("test4")
+0001 RETURN int(1)
+
+Test4::getSelfA:
+ ; (lines=3, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (self) (exception) string("A")
+0001 VERIFY_RETURN_TYPE T0
+0002 RETURN T0
+LIVE RANGES:
+ 0: 0001 - 0002 (tmp/var)
+
+Test4::getSelfB:
+ ; (lines=3, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (self) (exception) string("B")
+0001 VERIFY_RETURN_TYPE T0
+0002 RETURN T0
+LIVE RANGES:
+ 0: 0001 - 0002 (tmp/var)
+
+Test4::getStaticA:
+ ; (lines=3, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (static) (exception) string("A")
+0001 VERIFY_RETURN_TYPE T0
+0002 RETURN T0
+LIVE RANGES:
+ 0: 0001 - 0002 (tmp/var)
+
+Test4::getStaticB:
+ ; (lines=3, args=0, vars=0, tmps=1)
+ ; (after optimizer)
+ ; %s
+0000 T0 = FETCH_CLASS_CONSTANT (static) (exception) string("B")
+0001 VERIFY_RETURN_TYPE T0
+0002 RETURN T0
+LIVE RANGES:
+ 0: 0001 - 0002 (tmp/var)
+
+Test4::getTestIA:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)
+
+Test4::getTestIB:
+ ; (lines=1, args=0, vars=0, tmps=0)
+ ; (after optimizer)
+ ; %s
+0000 RETURN int(42)