Skip to content

Commit 814df43

Browse files
committed
Reimplemented used stack size recalculation (replaced 3d4babd)
1 parent af11f81 commit 814df43

File tree

3 files changed

+53
-52
lines changed

3 files changed

+53
-52
lines changed

ext/opcache/Optimizer/optimize_temp_vars_5.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,54 +38,6 @@
3838
max = i; \
3939
}
4040

41-
void optimize_adjust_fcall_stack_size(zend_op_array *callee, uint32_t delta, zend_optimizer_ctx *ctx) {
42-
zval *val;
43-
zend_op *start, *end;
44-
zend_op_array *op_array;
45-
zend_string *name;
46-
47-
if (!delta || !callee->function_name) {
48-
return;
49-
}
50-
51-
name = zend_string_tolower(callee->function_name);
52-
op_array = &ctx->script->main_op_array;
53-
start = op_array->opcodes;
54-
end = op_array->opcodes + op_array->last - 1;
55-
while (start < end) {
56-
if (start->opcode == ZEND_INIT_FCALL) {
57-
zval *zv = RT_CONSTANT(op_array, start->op2);
58-
if (Z_STR_P(zv) == name ||
59-
((Z_STRLEN_P(zv) == name->len) &&
60-
!memcmp(Z_STRVAL_P(zv), name->val, name->len))) {
61-
start->op1.num -= (delta * sizeof(zval));
62-
}
63-
}
64-
start++;
65-
}
66-
67-
ZEND_HASH_REVERSE_FOREACH_VAL(&ctx->script->function_table, val) {
68-
op_array = Z_PTR_P(val);
69-
if (op_array == callee) {
70-
continue; /* we can not break here */
71-
}
72-
start = op_array->opcodes;
73-
end = op_array->opcodes + op_array->last - 1;
74-
while (start < end) {
75-
if (start->opcode == ZEND_INIT_FCALL) {
76-
zval *zv = RT_CONSTANT(op_array, start->op2);
77-
if (Z_STR_P(zv) == name ||
78-
((Z_STRLEN_P(zv) == name->len) &&
79-
!memcmp(Z_STRVAL_P(zv), name->val, name->len))) {
80-
start->op1.num -= (delta * sizeof(zval));
81-
}
82-
}
83-
start++;
84-
}
85-
} ZEND_HASH_FOREACH_END();
86-
zend_string_release(name);
87-
}
88-
8941
void optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
9042
{
9143
int T = op_array->T;
@@ -228,8 +180,5 @@ void optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *c
228180
}
229181

230182
zend_arena_release(&ctx->arena, checkpoint);
231-
if (op_array->scope == NULL) {
232-
optimize_adjust_fcall_stack_size(op_array, op_array->T - (max + 1), ctx);
233-
}
234183
op_array->T = max + 1;
235184
}

ext/opcache/Optimizer/zend_optimizer.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,26 @@ static void zend_accel_optimize(zend_op_array *op_array,
498498
}
499499
}
500500

501+
static void zend_accel_adjust_fcall_stack_size(zend_op_array *op_array, zend_optimizer_ctx *ctx)
502+
{
503+
zend_function *func;
504+
zend_op *opline, *end;
505+
506+
opline = op_array->opcodes;
507+
end = opline + op_array->last;
508+
while (opline < end) {
509+
if (opline->opcode == ZEND_INIT_FCALL) {
510+
func = zend_hash_find_ptr(
511+
&ctx->script->function_table,
512+
Z_STR_P(RT_CONSTANT(op_array, opline->op2)));
513+
if (func) {
514+
opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, func);
515+
}
516+
}
517+
opline++;
518+
}
519+
}
520+
501521
int zend_accel_script_optimize(zend_persistent_script *script)
502522
{
503523
uint idx, j;
@@ -540,6 +560,38 @@ int zend_accel_script_optimize(zend_persistent_script *script)
540560
}
541561
}
542562

563+
if (ZEND_OPTIMIZER_PASS_12 & OPTIMIZATION_LEVEL) {
564+
zend_accel_adjust_fcall_stack_size(&script->main_op_array, &ctx);
565+
566+
for (idx = 0; idx < script->function_table.nNumUsed; idx++) {
567+
p = script->function_table.arData + idx;
568+
if (Z_TYPE(p->val) == IS_UNDEF) continue;
569+
op_array = (zend_op_array*)Z_PTR(p->val);
570+
zend_accel_adjust_fcall_stack_size(op_array, &ctx);
571+
}
572+
573+
for (idx = 0; idx < script->class_table.nNumUsed; idx++) {
574+
p = script->class_table.arData + idx;
575+
if (Z_TYPE(p->val) == IS_UNDEF) continue;
576+
ce = (zend_class_entry*)Z_PTR(p->val);
577+
for (j = 0; j < ce->function_table.nNumUsed; j++) {
578+
q = ce->function_table.arData + j;
579+
if (Z_TYPE(q->val) == IS_UNDEF) continue;
580+
op_array = (zend_op_array*)Z_PTR(q->val);
581+
if (op_array->scope == ce) {
582+
zend_accel_adjust_fcall_stack_size(op_array, &ctx);
583+
} else if (op_array->type == ZEND_USER_FUNCTION) {
584+
zend_op_array *orig_op_array;
585+
if ((orig_op_array = zend_hash_find_ptr(&op_array->scope->function_table, q->key)) != NULL) {
586+
HashTable *ht = op_array->static_variables;
587+
*op_array = *orig_op_array;
588+
op_array->static_variables = ht;
589+
}
590+
}
591+
}
592+
}
593+
}
594+
543595
if (ctx.constants) {
544596
zend_hash_destroy(ctx.constants);
545597
}

ext/opcache/Optimizer/zend_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define ZEND_OPTIMIZER_PASS_9 (1<<8) /* TMP VAR usage */
3737
#define ZEND_OPTIMIZER_PASS_10 (1<<9) /* NOP removal */
3838
#define ZEND_OPTIMIZER_PASS_11 (1<<10) /* Merge equal constants */
39-
#define ZEND_OPTIMIZER_PASS_12 (1<<11)
39+
#define ZEND_OPTIMIZER_PASS_12 (1<<11) /* Adjust used stack */
4040
#define ZEND_OPTIMIZER_PASS_13 (1<<12)
4141
#define ZEND_OPTIMIZER_PASS_14 (1<<13)
4242

0 commit comments

Comments
 (0)