Skip to content

String rc1 opts #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@ int zend_dfa_optimize_calls(zend_op_array *op_array, zend_ssa *ssa)
int var_num = ssa_op->op1_use;
zend_ssa_var *var = ssa->vars + var_num;

ZEND_ASSERT(ssa_op->op1_def < 0);
if (ssa_op->op1_def >= 0) {
zend_ssa_replace_op1_def_op1_use(ssa, ssa_op);
}

zend_ssa_unlink_use_chain(ssa, op_num, ssa_op->op1_use);
ssa_op->op1_use = -1;
ssa_op->op1_use_chain = -1;
Expand Down Expand Up @@ -1066,6 +1069,50 @@ static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ss
return 0;
}

/* Sets a flag on SEND ops when a copy can be a avoided. */
static void zend_dfa_optimize_send_copies(zend_op_array *op_array, zend_ssa *ssa)
{
/* func_get_args(), indirect accesses and exceptions could make the optimization observable.
* The latter two cases are already tested before applying the DFA pass. */
if (ssa->cfg.flags & ZEND_FUNC_VARARG) {
return;
}

for (uint32_t i = 0; i < op_array->last; i++) {
zend_op *opline = op_array->opcodes + i;
if ((opline->opcode != ZEND_SEND_VAR && opline->opcode != ZEND_SEND_VAR_EX) || opline->op2_type != IS_UNUSED || opline->op1_type != IS_CV) {
continue;
}

zend_ssa_op *ssa_op = ssa->ops + i;
int op1_def = ssa_op->op1_def;
if (op1_def == -1) {
continue;
}

int ssa_cv = ssa_op->op1_use;

/* Argument move must not be observable in backtraces */
if (ssa->vars[ssa_cv].var < op_array->num_args) {
continue;
}

/* Unsetting a CV is always fine if it gets overwritten afterwards.
* Since type inference often infers very wide types, we are very loose in matching types. */
uint32_t type = ssa->var_info[ssa_cv].type;
if ((type & (MAY_BE_REF|MAY_BE_UNDEF)) || !(type & MAY_BE_RC1) || !(type & (MAY_BE_STRING|MAY_BE_ARRAY))) {
continue;
}

zend_ssa_var *ssa_var = ssa->vars + op1_def;

if (ssa_var->no_val && !ssa_var->alias) {
/* Flag will be used by VM type spec handler */
opline->extended_value = 1;
}
}
}

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) {
Expand Down Expand Up @@ -1124,6 +1171,14 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
#endif
}

/* Optimization should not be done on main because of globals. */
if (op_array->function_name) {
zend_dfa_optimize_send_copies(op_array, ssa);
#if ZEND_DEBUG_DFA
ssa_verify_integrity(op_array, ssa, "after optimize send copies");
#endif
}

for (v = op_array->last_var; v < ssa->vars_count; v++) {

op_1 = ssa->vars[v].definition;
Expand Down
30 changes: 26 additions & 4 deletions Zend/Optimizer/sccp.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ typedef struct _sccp_ctx {
#define MAKE_TOP(zv) (Z_TYPE_INFO_P(zv) = TOP)
#define MAKE_BOT(zv) (Z_TYPE_INFO_P(zv) = BOT)

static void scp_dump_value(zval *zv) {
static void scp_dump_value(const zval *zv) {
if (IS_TOP(zv)) {
fprintf(stderr, " top");
} else if (IS_BOT(zv)) {
Expand Down Expand Up @@ -1050,6 +1050,12 @@ static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_o
case ZEND_SEND_VAL:
case ZEND_SEND_VAR:
{
SKIP_IF_TOP(op1);

if (opline->opcode == ZEND_SEND_VAR) {
SET_RESULT(op1, op1);
}

/* If the value of a SEND for an ICALL changes, we need to reconsider the
* ICALL result value. Otherwise we can ignore the opcode. */
zend_call_info *call;
Expand All @@ -1058,7 +1064,7 @@ static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_o
}

call = ctx->call_map[opline - ctx->scdf.op_array->opcodes];
if (IS_TOP(op1) || !call || !call->caller_call_opline
if (!call || !call->caller_call_opline
|| call->caller_call_opline->opcode != ZEND_DO_ICALL) {
return;
}
Expand Down Expand Up @@ -2034,8 +2040,14 @@ static int remove_call(sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op)
&ssa->ops[call->caller_init_opline - op_array->opcodes]);

for (i = 0; i < call->num_args; i++) {
zend_ssa_remove_instr(ssa, call->arg_info[i].opline,
&ssa->ops[call->arg_info[i].opline - op_array->opcodes]);
zend_op *op = call->arg_info[i].opline;
zend_ssa_op *this_ssa_op = &ssa->ops[op - op_array->opcodes];

if (op->opcode == ZEND_SEND_VAR && this_ssa_op->op1_def >= 0) {
zend_ssa_replace_op1_def_op1_use(ssa, this_ssa_op);
}

zend_ssa_remove_instr(ssa, op, this_ssa_op);
}

// TODO: remove call_info completely???
Expand Down Expand Up @@ -2188,6 +2200,10 @@ static int try_remove_definition(sccp_ctx *ctx, int var_num, zend_ssa_var *var,
return 0;
}

if (opline->opcode == ZEND_SEND_VAR) {
return 0;
}

/* Compound assign or incdec -> convert to direct ASSIGN */

if (!value) {
Expand Down Expand Up @@ -2330,6 +2346,12 @@ static int replace_constant_operands(sccp_ctx *ctx) {
FOREACH_USE(var, use) {
zend_op *opline = &op_array->opcodes[use];
zend_ssa_op *ssa_op = &ssa->ops[use];
/* Removing the def in try_remove_definition() may reduce optimisation opportunities.
* We want to keep the no_val definition until we actually replace it with a constant. */
if (opline->opcode == ZEND_SEND_VAR && ssa_op->op1_use == i && ssa_op->op1_def >= 0) {
zend_ssa_replace_op1_def_op1_use(ssa, ssa_op);
opline->extended_value = 0;
}
if (try_replace_op1(ctx, opline, ssa_op, i, value)) {
if (opline->opcode == ZEND_NOP) {
removed_ops++;
Expand Down
4 changes: 4 additions & 0 deletions Zend/Optimizer/zend_dfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ static zend_always_inline void _zend_dfg_add_use_def_op(const zend_op_array *op_
}
break;
case ZEND_SEND_VAR:
if (opline->op1_type == IS_CV && ((build_flags & ZEND_SSA_RC_INFERENCE) || opline->op2_type == IS_UNUSED)) {
goto add_op1_def;
}
break;
case ZEND_CAST:
case ZEND_QM_ASSIGN:
case ZEND_JMP_SET:
Expand Down
8 changes: 8 additions & 0 deletions Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,10 @@ static zend_always_inline zend_result _zend_update_type_info(
if (t1 & (MAY_BE_RC1|MAY_BE_REF)) {
tmp |= MAY_BE_RCN;
}
if ((t1 & (MAY_BE_ARRAY|MAY_BE_STRING)) && (t1 & MAY_BE_RC1) && !(t1 & (MAY_BE_UNDEF|MAY_BE_REF)) && ssa_vars[ssa_op->op1_def].no_val && !ssa_vars[ssa_op->op1_def].alias) {
/* implicit move may make value undef */
tmp |= MAY_BE_UNDEF;
}
UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
}
Expand Down Expand Up @@ -2991,6 +2995,10 @@ static zend_always_inline zend_result _zend_update_type_info(
case ZEND_SEND_FUNC_ARG:
if (ssa_op->op1_def >= 0) {
tmp = (t1 & MAY_BE_UNDEF)|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
if (opline->opcode == ZEND_SEND_VAR_EX && (t1 & (MAY_BE_ARRAY|MAY_BE_STRING)) && (t1 & MAY_BE_RC1) && !(t1 & (MAY_BE_UNDEF|MAY_BE_REF)) && ssa_vars[ssa_op->op1_def].no_val && !ssa_vars[ssa_op->op1_def].alias) {
/* implicit move may make value undef */
tmp |= MAY_BE_UNDEF;
}
UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
}
break;
Expand Down
14 changes: 14 additions & 0 deletions Zend/Optimizer/zend_ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ static zend_always_inline int _zend_ssa_rename_op(const zend_op_array *op_array,
}
break;
case ZEND_SEND_VAR:
if (opline->op1_type == IS_CV && ((build_flags & ZEND_SSA_RC_INFERENCE) || opline->op2_type == IS_UNUSED)) {
goto add_op1_def;
}
break;
case ZEND_CAST:
case ZEND_QM_ASSIGN:
case ZEND_JMP_SET:
Expand Down Expand Up @@ -1680,3 +1684,13 @@ void zend_ssa_rename_var_uses(zend_ssa *ssa, int old, int new, bool update_types
old_var->phi_use_chain = NULL;
}
/* }}} */

void zend_ssa_replace_op1_def_op1_use(zend_ssa *ssa, zend_ssa_op *ssa_op)
{
int op1_new = ssa_op->op1_use;
ZEND_ASSERT(op1_new >= 0);
ZEND_ASSERT(ssa_op->op1_def >= 0);
/* zend_ssa_rename_var_uses() clear use_chain & phi_use_chain for us */
zend_ssa_rename_var_uses(ssa, ssa_op->op1_def, op1_new, true);
zend_ssa_remove_op1_def(ssa, ssa_op);
}
1 change: 1 addition & 0 deletions Zend/Optimizer/zend_ssa.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void zend_ssa_remove_uses_of_var(zend_ssa *ssa, int var_num);
void zend_ssa_remove_block(zend_op_array *op_array, zend_ssa *ssa, int b);
void zend_ssa_rename_var_uses(zend_ssa *ssa, int old_var, int new_var, bool update_types);
void zend_ssa_remove_block_from_cfg(zend_ssa *ssa, int b);
void zend_ssa_replace_op1_def_op1_use(zend_ssa *ssa, zend_ssa_op *ssa_op);

static zend_always_inline void _zend_ssa_remove_def(zend_ssa_var *var)
{
Expand Down
66 changes: 66 additions & 0 deletions Zend/array_micro_bench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

function array_merge_loop($n) {
$a = range(1, 3);
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2, 3]);
}
}

function array_merge_loop_with_temporary($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2]) + [100];
}
}

function array_merge_loop_nested($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2]) + [$a];
}
}

function array_unique_loop($n) {
$a = range(1, 500);
$a += $a;
for ($i = 0; $i < $n; ++$i) {
$a = array_unique($a);
}
}

function user_func_helper($array) {
for ($i = 0; $i < 100; $i++) {
$array[$i] = $i;
}
return $array;
}

function user_func($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = user_func_helper($a);
}
}

/*****/

require 'bench_common.php';

const N = 20000;

$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
array_merge_loop(N);
$t = end_test($t, 'array_merge', $overhead);
array_merge_loop_with_temporary(N);
$t = end_test($t, 'array_merge temp', $overhead);
array_merge_loop_nested(N);
$t = end_test($t, 'array_merge nest', $overhead);
array_unique_loop(N);
$t = end_test($t, 'array_unique', $overhead);
user_func(N);
$t = end_test($t, 'user_func', $overhead);
total($t0, "Total");
36 changes: 1 addition & 35 deletions Zend/bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,41 +345,7 @@ function strcat($n) {

/*****/

function gethrtime()
{
$hrtime = hrtime();
return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
}

function start_test()
{
ob_start();
return gethrtime();
}

function end_test($start, $name)
{
global $total;
$end = gethrtime();
ob_end_clean();
$total += $end-$start;
$num = number_format($end-$start,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));

echo $name.$pad.$num."\n";
ob_start();
return gethrtime();
}

function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
require 'bench_common.php';

$t0 = $t = start_test();
simple();
Expand Down
48 changes: 48 additions & 0 deletions Zend/bench_common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}

function gethrtime()
{
$hrtime = hrtime();
return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
}

function start_test()
{
ob_start();
return gethrtime();
}

function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = gethrtime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead,3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return gethrtime();
}

function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
Loading