Skip to content

Commit 0d9269d

Browse files
committed
Extract helper for fetching class entry in optimizer
This code is repeated a few time. Two occurrences additionally contained checks for user classes in CG(class_table) with the same file name, but as far as I know these should always be in the script class_table, so I'm omitting the check here.
1 parent d4ead60 commit 0d9269d

File tree

6 files changed

+31
-61
lines changed

6 files changed

+31
-61
lines changed

Zend/Optimizer/escape_analysis.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,6 @@ static int zend_build_equi_escape_sets(int *parent, zend_op_array *op_array, zen
147147
}
148148
/* }}} */
149149

150-
static inline zend_class_entry *get_class_entry(const zend_script *script, zend_string *lcname) /* {{{ */
151-
{
152-
zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
153-
if (ce) {
154-
return ce;
155-
}
156-
157-
ce = zend_hash_find_ptr(CG(class_table), lcname);
158-
if (ce && ce->type == ZEND_INTERNAL_CLASS) {
159-
return ce;
160-
}
161-
162-
return NULL;
163-
}
164-
/* }}} */
165-
166150
static int is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
167151
{
168152
zend_ssa_op *ssa_op = ssa->ops + def;
@@ -175,7 +159,8 @@ static int is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, in
175159
case ZEND_NEW:
176160
/* objects with destructors should escape */
177161
if (opline->op1_type == IS_CONST) {
178-
zend_class_entry *ce = get_class_entry(script, Z_STR_P(CRT_CONSTANT(opline->op1)+1));
162+
zend_class_entry *ce = zend_optimizer_get_class_entry(
163+
script, Z_STR_P(CRT_CONSTANT(opline->op1)+1));
179164
uint32_t forbidden_flags =
180165
/* These flags will always cause an exception */
181166
ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS
@@ -242,7 +227,8 @@ static int is_local_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var
242227
case ZEND_NEW:
243228
/* objects with destructors should escape */
244229
if (opline->op1_type == IS_CONST) {
245-
zend_class_entry *ce = get_class_entry(script, Z_STR_P(CRT_CONSTANT(opline->op1)+1));
230+
zend_class_entry *ce = zend_optimizer_get_class_entry(
231+
script, Z_STR_P(CRT_CONSTANT(opline->op1)+1));
246232
if (ce && !ce->create_object && !ce->constructor &&
247233
!ce->destructor && !ce->__get && !ce->__set && !ce->parent) {
248234
return 1;

Zend/Optimizer/pass1.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,9 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
238238
zend_string_equals_ci(Z_STR(ZEND_OP1_LITERAL(opline)), op_array->scope->name)) {
239239
ce = op_array->scope;
240240
} else {
241-
if ((ce = zend_hash_find_ptr(EG(class_table),
242-
Z_STR(op_array->literals[opline->op1.constant + 1]))) == NULL ||
243-
(ce->type == ZEND_INTERNAL_CLASS &&
244-
ce->info.internal.module->type != MODULE_PERSISTENT) ||
245-
(ce->type == ZEND_USER_CLASS &&
246-
ce->info.user.filename != op_array->filename)) {
241+
ce = zend_optimizer_get_class_entry(
242+
ctx->script, Z_STR(op_array->literals[opline->op1.constant + 1]));
243+
if (!ce) {
247244
break;
248245
}
249246
}

Zend/Optimizer/zend_inference.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "zend_func_info.h"
2323
#include "zend_call_graph.h"
2424
#include "zend_worklist.h"
25+
#include "zend_optimizer_internal.h"
2526

2627
/* The used range inference algorithm is described in:
2728
* V. Campos, R. Rodrigues, I. de Assis Costa and F. Pereira.
@@ -2189,20 +2190,6 @@ static uint32_t binary_op_result_type(
21892190
return tmp;
21902191
}
21912192

2192-
static inline zend_class_entry *get_class_entry(const zend_script *script, zend_string *lcname) {
2193-
zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
2194-
if (ce) {
2195-
return ce;
2196-
}
2197-
2198-
ce = zend_hash_find_ptr(CG(class_table), lcname);
2199-
if (ce && ce->type == ZEND_INTERNAL_CLASS) {
2200-
return ce;
2201-
}
2202-
2203-
return NULL;
2204-
}
2205-
22062193
static uint32_t zend_convert_type_declaration_mask(uint32_t type_mask) {
22072194
uint32_t result_mask = type_mask & MAY_BE_ANY;
22082195
if (type_mask & MAY_BE_VOID) {
@@ -2238,7 +2225,7 @@ ZEND_API uint32_t zend_fetch_arg_info_type(const zend_script *script, zend_arg_i
22382225
/* As we only have space to store one CE, we use a plain object type for class unions. */
22392226
if (ZEND_TYPE_HAS_NAME(arg_info->type)) {
22402227
zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(arg_info->type));
2241-
*pce = get_class_entry(script, lcname);
2228+
*pce = zend_optimizer_get_class_entry(script, lcname);
22422229
zend_string_release_ex(lcname, 0);
22432230
}
22442231
}
@@ -2320,7 +2307,7 @@ static zend_property_info *zend_fetch_static_prop_info(const zend_script *script
23202307
}
23212308
} else if (opline->op2_type == IS_CONST) {
23222309
zval *zv = CRT_CONSTANT(opline->op2);
2323-
ce = get_class_entry(script, Z_STR_P(zv + 1));
2310+
ce = zend_optimizer_get_class_entry(script, Z_STR_P(zv + 1));
23242311
}
23252312

23262313
if (ce) {
@@ -2348,7 +2335,7 @@ static uint32_t zend_fetch_prop_type(const zend_script *script, zend_property_in
23482335
*pce = ZEND_TYPE_CE(prop_info->type);
23492336
} else if (ZEND_TYPE_HAS_NAME(prop_info->type)) {
23502337
zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(prop_info->type));
2351-
*pce = get_class_entry(script, lcname);
2338+
*pce = zend_optimizer_get_class_entry(script, lcname);
23522339
zend_string_release(lcname);
23532340
}
23542341
}
@@ -3106,7 +3093,7 @@ static zend_always_inline int _zend_update_type_info(
31063093
} else if (opline->op2_type == IS_CONST) {
31073094
zval *zv = CRT_CONSTANT(opline->op2);
31083095
if (Z_TYPE_P(zv) == IS_STRING) {
3109-
ce = get_class_entry(script, Z_STR_P(zv+1));
3096+
ce = zend_optimizer_get_class_entry(script, Z_STR_P(zv+1));
31103097
UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_op->result_def);
31113098
} else {
31123099
UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
@@ -3118,7 +3105,7 @@ static zend_always_inline int _zend_update_type_info(
31183105
case ZEND_NEW:
31193106
tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT;
31203107
if (opline->op1_type == IS_CONST &&
3121-
(ce = get_class_entry(script, Z_STR_P(CRT_CONSTANT(opline->op1)+1))) != NULL) {
3108+
(ce = zend_optimizer_get_class_entry(script, Z_STR_P(CRT_CONSTANT(opline->op1)+1))) != NULL) {
31223109
UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_op->result_def);
31233110
} else if ((t1 & MAY_BE_CLASS) && ssa_op->op1_use >= 0 && ssa_var_info[ssa_op->op1_use].ce) {
31243111
UPDATE_SSA_OBJ_TYPE(ssa_var_info[ssa_op->op1_use].ce, ssa_var_info[ssa_op->op1_use].is_instanceof, ssa_op->result_def);

Zend/Optimizer/zend_optimizer.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -754,24 +754,26 @@ void zend_optimizer_shift_jump(zend_op_array *op_array, zend_op *opline, uint32_
754754
}
755755
}
756756

757+
zend_class_entry *zend_optimizer_get_class_entry(const zend_script *script, zend_string *lcname) {
758+
zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
759+
if (ce) {
760+
return ce;
761+
}
762+
763+
ce = zend_hash_find_ptr(CG(class_table), lcname);
764+
if (ce && ce->type == ZEND_INTERNAL_CLASS) {
765+
return ce;
766+
}
767+
768+
return NULL;
769+
}
770+
757771
static zend_class_entry *get_class_entry_from_op1(
758772
zend_script *script, zend_op_array *op_array, zend_op *opline) {
759773
if (opline->op1_type == IS_CONST) {
760774
zval *op1 = CRT_CONSTANT(opline->op1);
761775
if (Z_TYPE_P(op1) == IS_STRING) {
762-
zend_string *class_name = Z_STR_P(op1 + 1);
763-
zend_class_entry *ce;
764-
if (script && (ce = zend_hash_find_ptr(&script->class_table, class_name))) {
765-
return ce;
766-
} else if ((ce = zend_hash_find_ptr(EG(class_table), class_name))) {
767-
if (ce->type == ZEND_INTERNAL_CLASS) {
768-
return ce;
769-
} else if (ce->type == ZEND_USER_CLASS &&
770-
ce->info.user.filename &&
771-
ce->info.user.filename == op_array->filename) {
772-
return ce;
773-
}
774-
}
776+
return zend_optimizer_get_class_entry(script, Z_STR_P(op1 + 1));
775777
}
776778
} else if (opline->op1_type == IS_UNUSED && op_array->scope
777779
&& !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)

Zend/Optimizer/zend_optimizer_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ int zend_optimizer_replace_by_const(zend_op_array *op_array,
9696
uint32_t var,
9797
zval *val);
9898
zend_op *zend_optimizer_get_loop_var_def(const zend_op_array *op_array, zend_op *free_opline);
99+
zend_class_entry *zend_optimizer_get_class_entry(const zend_script *script, zend_string *lcname);
99100

100101
void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx);
101102
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx);

Zend/Optimizer/zend_ssa.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,9 @@ static void place_essa_pis(
535535
(opline-1)->op2_type == IS_CONST) {
536536
int var = EX_VAR_TO_NUM((opline-1)->op1.var);
537537
zend_string *lcname = Z_STR_P(CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2) + 1);
538-
zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
538+
zend_class_entry *ce = zend_optimizer_get_class_entry(script, lcname);
539539
if (!ce) {
540-
ce = zend_hash_find_ptr(CG(class_table), lcname);
541-
if (!ce || ce->type != ZEND_INTERNAL_CLASS) {
542-
continue;
543-
}
540+
continue;
544541
}
545542

546543
if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {

0 commit comments

Comments
 (0)