php-src/ext/opcache/Optimizer/zend_optimizer.c

901 lines
26 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
2015-01-15 15:27:30 +00:00
| Copyright (c) 1998-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
| Stanislav Malyshev <stas@zend.com> |
| Dmitry Stogov <dmitry@zend.com> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "Optimizer/zend_optimizer.h"
#include "Optimizer/zend_optimizer_internal.h"
#include "zend_API.h"
#include "zend_constants.h"
#include "zend_execute.h"
#include "zend_vm.h"
2015-12-09 11:52:00 +00:00
#include "zend_cfg.h"
#include "zend_func_info.h"
2015-12-24 11:30:41 +00:00
#include "zend_call_graph.h"
2015-12-09 11:52:00 +00:00
#include "zend_dump.h"
2015-12-24 11:30:41 +00:00
#ifndef HAVE_DFA_PASS
# define HAVE_DFA_PASS 0
#endif
static void zend_optimizer_zval_dtor_wrapper(zval *zvalue)
{
zval_dtor(zvalue);
}
void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value)
{
zval val;
if (!ctx->constants) {
ctx->constants = zend_arena_alloc(&ctx->arena, sizeof(HashTable));
zend_hash_init(ctx->constants, 16, NULL, zend_optimizer_zval_dtor_wrapper, 0);
}
2014-03-28 19:34:49 +00:00
ZVAL_DUP(&val, value);
zend_hash_add(ctx->constants, Z_STR_P(name), &val);
}
int zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value)
{
zval *val;
2014-03-28 19:34:49 +00:00
if ((val = zend_hash_find(constants, Z_STR_P(name))) != NULL) {
ZVAL_DUP(value, val);
return 1;
}
return 0;
}
int zend_optimizer_lookup_cv(zend_op_array *op_array, zend_string* name)
{
int i = 0;
2014-08-25 17:24:55 +00:00
zend_ulong hash_value = zend_string_hash_val(name);
while (i < op_array->last_var) {
2014-03-28 19:34:49 +00:00
if (op_array->vars[i] == name ||
(ZSTR_H(op_array->vars[i]) == hash_value &&
ZSTR_LEN(op_array->vars[i]) == ZSTR_LEN(name) &&
memcmp(ZSTR_VAL(op_array->vars[i]), ZSTR_VAL(name), ZSTR_LEN(name)) == 0)) {
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
}
i++;
}
i = op_array->last_var;
op_array->last_var++;
2014-03-28 19:34:49 +00:00
op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_string*));
2014-08-25 17:24:55 +00:00
op_array->vars[i] = zend_string_dup(name, 0);
2014-03-31 14:13:16 +00:00
/* all IS_TMP_VAR and IS_VAR variable numbers have to be adjusted */
{
zend_op *opline = op_array->opcodes;
zend_op *end = opline + op_array->last;
while (opline < end) {
if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
opline->op1.var += sizeof(zval);
}
if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
opline->op2.var += sizeof(zval);
}
if (opline->result_type & (IS_TMP_VAR|IS_VAR)) {
opline->result.var += sizeof(zval);
}
opline++;
}
}
2015-01-03 09:22:58 +00:00
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
}
2014-12-13 22:06:14 +00:00
int zend_optimizer_add_literal(zend_op_array *op_array, zval *zv)
{
int i = op_array->last_literal;
op_array->last_literal++;
op_array->literals = (zval*)erealloc(op_array->literals, op_array->last_literal * sizeof(zval));
ZVAL_COPY_VALUE(&op_array->literals[i], zv);
Z_CACHE_SLOT(op_array->literals[i]) = -1;
return i;
}
static inline int zend_optimizer_add_literal_string(zend_op_array *op_array, zend_string *str) {
zval zv;
ZVAL_STR(&zv, str);
zend_string_hash_val(str);
return zend_optimizer_add_literal(op_array, &zv);
}
2015-03-27 09:50:36 +00:00
int zend_optimizer_is_disabled_func(const char *name, size_t len) {
zend_function *fbc = (zend_function *)zend_hash_str_find_ptr(EG(function_table), name, len);
return (fbc && fbc->type == ZEND_INTERNAL_FUNCTION &&
2015-03-27 09:50:36 +00:00
fbc->internal_function.handler == ZEND_FN(display_disabled_function));
}
static inline void drop_leading_backslash(zval *val) {
if (Z_STRVAL_P(val)[0] == '\\') {
zend_string *str = zend_string_init(Z_STRVAL_P(val) + 1, Z_STRLEN_P(val) - 1, 0);
zval_dtor(val);
ZVAL_STR(val, str);
}
}
static inline void alloc_cache_slots_op1(zend_op_array *op_array, zend_op *opline, uint32_t num) {
Z_CACHE_SLOT(op_array->literals[opline->op1.constant]) = op_array->cache_size;
op_array->cache_size += num * sizeof(void *);
}
static inline void alloc_cache_slots_op2(zend_op_array *op_array, zend_op *opline, uint32_t num) {
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
op_array->cache_size += num * sizeof(void *);
}
2015-11-07 16:45:26 +00:00
#define REQUIRES_STRING(val) do { \
if (Z_TYPE_P(val) != IS_STRING) { \
zval_dtor(val); \
return 0; \
} \
} while (0)
#define TO_STRING_NOWARN(val) do { \
if (Z_TYPE_P(val) >= IS_ARRAY) { \
zval_dtor(val); \
return 0; \
} \
convert_to_string(val); \
} while (0)
int zend_optimizer_update_op1_const(zend_op_array *op_array,
zend_op *opline,
zval *val)
{
switch (opline->opcode) {
case ZEND_FREE:
MAKE_NOP(opline);
zval_dtor(val);
return 1;
case ZEND_INIT_STATIC_METHOD_CALL:
case ZEND_CATCH:
case ZEND_FETCH_CONSTANT:
case ZEND_FETCH_CLASS_CONSTANT:
case ZEND_DEFINED:
case ZEND_NEW:
2015-11-07 16:45:26 +00:00
REQUIRES_STRING(val);
drop_leading_backslash(val);
2014-12-13 22:06:14 +00:00
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
alloc_cache_slots_op1(op_array, opline, 1);
zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
break;
case ZEND_FETCH_STATIC_PROP_R:
case ZEND_FETCH_STATIC_PROP_W:
case ZEND_FETCH_STATIC_PROP_RW:
case ZEND_FETCH_STATIC_PROP_IS:
case ZEND_FETCH_STATIC_PROP_UNSET:
case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
TO_STRING_NOWARN(val);
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
alloc_cache_slots_op1(op_array, opline, 2);
break;
case ZEND_CONCAT:
case ZEND_FAST_CONCAT:
case ZEND_FETCH_R:
case ZEND_FETCH_W:
case ZEND_FETCH_RW:
case ZEND_FETCH_IS:
case ZEND_FETCH_UNSET:
case ZEND_FETCH_FUNC_ARG:
2015-11-07 16:45:26 +00:00
TO_STRING_NOWARN(val);
/* break missing intentionally */
default:
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
break;
}
ZEND_OP1_TYPE(opline) = IS_CONST;
if (Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING) {
zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline)));
}
return 1;
}
int zend_optimizer_update_op2_const(zend_op_array *op_array,
zend_op *opline,
zval *val)
{
switch (opline->opcode) {
case ZEND_ASSIGN_REF:
zval_dtor(val);
return 0;
case ZEND_FETCH_CLASS:
case ZEND_INIT_FCALL_BY_NAME:
/*case ZEND_INIT_NS_FCALL_BY_NAME:*/
case ZEND_ADD_INTERFACE:
case ZEND_ADD_TRAIT:
case ZEND_INSTANCEOF:
case ZEND_FETCH_STATIC_PROP_R:
case ZEND_FETCH_STATIC_PROP_W:
case ZEND_FETCH_STATIC_PROP_RW:
case ZEND_FETCH_STATIC_PROP_IS:
case ZEND_FETCH_STATIC_PROP_UNSET:
case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
case ZEND_UNSET_STATIC_PROP:
case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2015-11-07 16:45:26 +00:00
REQUIRES_STRING(val);
drop_leading_backslash(val);
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
alloc_cache_slots_op2(op_array, opline, 1);
break;
case ZEND_INIT_FCALL:
2015-11-07 16:45:26 +00:00
REQUIRES_STRING(val);
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
alloc_cache_slots_op2(op_array, opline, 1);
break;
case ZEND_INIT_DYNAMIC_CALL:
if (Z_TYPE_P(val) == IS_STRING) {
2015-11-07 16:45:26 +00:00
if (zend_memrchr(Z_STRVAL_P(val), ':', Z_STRLEN_P(val))) {
zval_dtor(val);
return 0;
}
opline->opcode = ZEND_INIT_FCALL_BY_NAME;
drop_leading_backslash(val);
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
alloc_cache_slots_op2(op_array, opline, 1);
} else {
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
}
break;
case ZEND_INIT_METHOD_CALL:
case ZEND_INIT_STATIC_METHOD_CALL:
2015-11-07 16:45:26 +00:00
REQUIRES_STRING(val);
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
2015-11-07 16:45:26 +00:00
zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
alloc_cache_slots_op2(op_array, opline, 2);
break;
/*case ZEND_FETCH_CLASS_CONSTANT:*/
case ZEND_ASSIGN_OBJ:
case ZEND_FETCH_OBJ_R:
case ZEND_FETCH_OBJ_W:
case ZEND_FETCH_OBJ_RW:
case ZEND_FETCH_OBJ_IS:
case ZEND_FETCH_OBJ_UNSET:
case ZEND_FETCH_OBJ_FUNC_ARG:
case ZEND_UNSET_OBJ:
case ZEND_PRE_INC_OBJ:
case ZEND_PRE_DEC_OBJ:
case ZEND_POST_INC_OBJ:
case ZEND_POST_DEC_OBJ:
case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2015-11-07 16:45:26 +00:00
TO_STRING_NOWARN(val);
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
2015-11-07 16:45:26 +00:00
alloc_cache_slots_op2(op_array, opline, 2);
break;
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
case ZEND_ASSIGN_POW:
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
case ZEND_ASSIGN_CONCAT:
case ZEND_ASSIGN_BW_OR:
case ZEND_ASSIGN_BW_AND:
case ZEND_ASSIGN_BW_XOR:
2015-11-07 16:45:26 +00:00
if (opline->extended_value == ZEND_ASSIGN_OBJ) {
TO_STRING_NOWARN(val);
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
alloc_cache_slots_op2(op_array, opline, 2);
} else {
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
}
break;
case ZEND_OP_DATA:
if ((opline-1)->opcode != ZEND_ASSIGN_DIM &&
((opline-1)->extended_value != ZEND_ASSIGN_DIM ||
((opline-1)->opcode != ZEND_ASSIGN_ADD &&
(opline-1)->opcode != ZEND_ASSIGN_SUB &&
(opline-1)->opcode != ZEND_ASSIGN_MUL &&
(opline-1)->opcode != ZEND_ASSIGN_DIV &&
(opline-1)->opcode != ZEND_ASSIGN_POW &&
(opline-1)->opcode != ZEND_ASSIGN_MOD &&
(opline-1)->opcode != ZEND_ASSIGN_SL &&
(opline-1)->opcode != ZEND_ASSIGN_SR &&
(opline-1)->opcode != ZEND_ASSIGN_CONCAT &&
(opline-1)->opcode != ZEND_ASSIGN_BW_OR &&
(opline-1)->opcode != ZEND_ASSIGN_BW_AND &&
(opline-1)->opcode != ZEND_ASSIGN_BW_XOR))
) {
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
break;
}
/* break missing intentionally */
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
case ZEND_ADD_ARRAY_ELEMENT:
case ZEND_INIT_ARRAY:
case ZEND_ASSIGN_DIM:
case ZEND_UNSET_DIM:
case ZEND_FETCH_DIM_R:
case ZEND_FETCH_DIM_W:
case ZEND_FETCH_DIM_RW:
case ZEND_FETCH_DIM_IS:
case ZEND_FETCH_DIM_FUNC_ARG:
case ZEND_FETCH_DIM_UNSET:
case ZEND_FETCH_LIST:
if (Z_TYPE_P(val) == IS_STRING) {
zend_ulong index;
if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) {
zval_dtor(val);
ZVAL_LONG(val, index);
}
}
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
break;
case ZEND_ROPE_INIT:
case ZEND_ROPE_ADD:
case ZEND_ROPE_END:
case ZEND_CONCAT:
case ZEND_FAST_CONCAT:
2015-11-07 16:45:26 +00:00
TO_STRING_NOWARN(val);
/* break missing intentionally */
default:
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
break;
}
ZEND_OP2_TYPE(opline) = IS_CONST;
if (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) {
zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline)));
}
return 1;
}
void zend_optimizer_remove_live_range(zend_op_array *op_array, uint32_t var)
{
if (op_array->last_live_range) {
int i = 0;
int j = 0;
uint32_t *map;
ALLOCA_FLAG(use_heap);
map = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last_live_range, use_heap);
do {
if ((op_array->live_range[i].var & ~ZEND_LIVE_MASK) != var) {
map[i] = j;
if (i != j) {
op_array->live_range[j] = op_array->live_range[i];
}
j++;
}
i++;
} while (i < op_array->last_live_range);
if (i != j) {
zend_op *opline = op_array->opcodes;
zend_op *end = opline + op_array->last;
op_array->last_live_range = j;
while (opline != end) {
if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) &&
opline->extended_value == ZEND_FREE_ON_RETURN) {
opline->op2.num = map[opline->op2.num];
}
opline++;
}
}
2015-12-07 15:25:25 +00:00
free_alloca(map, use_heap);
}
}
int zend_optimizer_replace_by_const(zend_op_array *op_array,
zend_op *opline,
zend_uchar type,
uint32_t var,
2014-12-13 22:06:14 +00:00
zval *val)
{
zend_op *end = op_array->opcodes + op_array->last;
while (opline < end) {
if (ZEND_OP1_TYPE(opline) == type &&
ZEND_OP1(opline).var == var) {
switch (opline->opcode) {
case ZEND_FETCH_DIM_W:
case ZEND_FETCH_DIM_RW:
case ZEND_FETCH_DIM_FUNC_ARG:
case ZEND_FETCH_DIM_UNSET:
case ZEND_ASSIGN_DIM:
case ZEND_SEPARATE:
2015-11-07 16:45:26 +00:00
case ZEND_RETURN_BY_REF:
zval_dtor(val);
return 0;
2014-11-18 11:37:36 +00:00
case ZEND_SEND_VAR:
opline->extended_value = 0;
opline->opcode = ZEND_SEND_VAL;
break;
case ZEND_SEND_VAR_EX:
opline->extended_value = 0;
opline->opcode = ZEND_SEND_VAL_EX;
break;
case ZEND_SEND_VAR_NO_REF:
if (opline->extended_value & ZEND_ARG_COMPILE_TIME_BOUND) {
if (opline->extended_value & ZEND_ARG_SEND_BY_REF) {
2015-03-20 02:24:04 +00:00
zval_dtor(val);
return 0;
}
opline->extended_value = 0;
opline->opcode = ZEND_SEND_VAL_EX;
} else {
opline->extended_value = 0;
opline->opcode = ZEND_SEND_VAL;
}
break;
2015-11-07 16:45:26 +00:00
case ZEND_SEND_USER:
opline->opcode = ZEND_SEND_VAL_EX;
break;
/* In most cases IS_TMP_VAR operand may be used only once.
* The operands are usually destroyed by the opcode handler.
* ZEND_CASE is an exception, that keeps operand unchanged,
* and allows its reuse. The number of ZEND_CASE instructions
* usually terminated by ZEND_FREE that finally kills the value.
*/
case ZEND_FREE:
case ZEND_CASE: {
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
zend_op *m, *n;
int brk = op_array->last_live_range;
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
zend_bool in_switch = 0;
while (brk--) {
2015-11-18 15:04:02 +00:00
if (op_array->live_range[brk].start <= (uint32_t)(opline - op_array->opcodes) &&
op_array->live_range[brk].end > (uint32_t)(opline - op_array->opcodes)) {
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
in_switch = 1;
break;
}
}
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
if (!in_switch) {
ZEND_ASSERT(opline->opcode == ZEND_FREE);
MAKE_NOP(opline);
zval_dtor(val);
return 1;
}
m = opline;
n = op_array->opcodes + op_array->live_range[brk].end;
if (n->opcode == ZEND_FREE &&
!(n->extended_value & ZEND_FREE_ON_RETURN)) {
n++;
} else {
n = op_array->opcodes + op_array->last;
}
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
while (m < n) {
if (ZEND_OP1_TYPE(m) == type &&
ZEND_OP1(m).var == var) {
if (m->opcode == ZEND_CASE) {
zval old_val;
ZVAL_COPY_VALUE(&old_val, val);
zval_copy_ctor(val);
zend_optimizer_update_op1_const(op_array, m, val);
ZVAL_COPY_VALUE(val, &old_val);
} else if (m->opcode == ZEND_FREE) {
MAKE_NOP(m);
} else {
ZEND_ASSERT(0);
}
}
m++;
}
zval_dtor(val);
zend_optimizer_remove_live_range(op_array, var);
return 1;
}
case ZEND_VERIFY_RETURN_TYPE: {
zend_arg_info *ret_info = op_array->arg_info - 1;
ZEND_ASSERT((opline + 1)->opcode == ZEND_RETURN || (opline + 1)->opcode == ZEND_RETURN_BY_REF);
if (ret_info->class_name
|| ret_info->type_hint == IS_CALLABLE
|| !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(val))
|| (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
zval_dtor(val);
return 0;
}
MAKE_NOP(opline);
opline++;
break;
}
default:
break;
2015-01-03 09:22:58 +00:00
}
if (zend_optimizer_update_op1_const(op_array, opline, val)) {
zend_optimizer_remove_live_range(op_array, var);
return 1;
}
return 0;
}
2015-01-03 09:22:58 +00:00
if (ZEND_OP2_TYPE(opline) == type &&
ZEND_OP2(opline).var == var) {
if (zend_optimizer_update_op2_const(op_array, opline, val)) {
zend_optimizer_remove_live_range(op_array, var);
return 1;
}
return 0;
}
opline++;
}
return 1;
}
static void zend_optimize(zend_op_array *op_array,
2014-12-13 22:06:14 +00:00
zend_optimizer_ctx *ctx)
{
if (op_array->type == ZEND_EVAL_CODE) {
return;
}
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_BEFORE_OPTIMIZER) {
zend_dump_op_array(op_array, 0, "before optimizer", NULL);
2015-12-09 11:52:00 +00:00
}
/* pass 1
* - substitute persistent constants (true, false, null, etc)
* - perform compile-time evaluation of constant binary and unary operations
* - optimize series of ADD_STRING and/or ADD_CHAR
* - convert CAST(IS_BOOL,x) into BOOL(x)
*/
if (ZEND_OPTIMIZER_PASS_1 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
zend_optimizer_pass1(op_array, ctx);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_1) {
zend_dump_op_array(op_array, 0, "after pass 1", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 2:
* - convert non-numeric constants to numeric constants in numeric operators
* - optimize constant conditional JMPs
* - optimize static BRKs and CONTs
* - pre-evaluate constant function calls
*/
if (ZEND_OPTIMIZER_PASS_2 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
zend_optimizer_pass2(op_array);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_2) {
zend_dump_op_array(op_array, 0, "after pass 2", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 3:
* - optimize $i = $i+expr to $i+=expr
* - optimize series of JMPs
* - change $i++ to ++$i where possible
*/
if (ZEND_OPTIMIZER_PASS_3 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
zend_optimizer_pass3(op_array);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_3) {
zend_dump_op_array(op_array, 0, "after pass 1", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 4:
* - INIT_FCALL_BY_NAME -> DO_FCALL
*/
if (ZEND_OPTIMIZER_PASS_4 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
optimize_func_calls(op_array, ctx);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_4) {
zend_dump_op_array(op_array, 0, "after pass 1", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 5:
* - CFG optimization
*/
if (ZEND_OPTIMIZER_PASS_5 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
optimize_cfg(op_array, ctx);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_5) {
zend_dump_op_array(op_array, 0, "after pass 5", NULL);
2015-12-09 11:52:00 +00:00
}
}
2015-12-24 11:30:41 +00:00
#if HAVE_DFA_PASS
/* pass 6:
* - DFA optimization
*/
2015-12-24 11:30:41 +00:00
if ((ZEND_OPTIMIZER_PASS_6 & ctx->optimization_level) &&
!(ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level)) {
optimize_dfa(op_array, ctx);
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_6) {
zend_dump_op_array(op_array, 0, "after pass 6", NULL);
}
}
2015-12-24 11:30:41 +00:00
#endif
/* pass 9:
* - Optimize temp variables usage
*/
if (ZEND_OPTIMIZER_PASS_9 & ctx->optimization_level) {
optimize_temporary_variables(op_array, ctx);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_9) {
zend_dump_op_array(op_array, 0, "after pass 9", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 10:
* - remove NOPs
*/
if (((ZEND_OPTIMIZER_PASS_10|ZEND_OPTIMIZER_PASS_5) & ctx->optimization_level) == ZEND_OPTIMIZER_PASS_10) {
zend_optimizer_nop_removal(op_array);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_10) {
zend_dump_op_array(op_array, 0, "after pass 10", NULL);
2015-12-09 11:52:00 +00:00
}
}
/* pass 11:
2015-01-03 09:22:58 +00:00
* - Compact literals table
*/
if (ZEND_OPTIMIZER_PASS_11 & ctx->optimization_level) {
2014-12-13 22:06:14 +00:00
zend_optimizer_compact_literals(op_array, ctx);
2015-12-09 11:52:00 +00:00
if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_11) {
zend_dump_op_array(op_array, 0, "after pass 11", NULL);
2015-12-09 11:52:00 +00:00
}
}
if (ctx->debug_level & ZEND_DUMP_AFTER_OPTIMIZER) {
zend_dump_op_array(op_array, 0, "after optimizer", NULL);
}
}
2015-12-24 11:30:41 +00:00
static void zend_revert_pass_two(zend_op_array *op_array)
{
zend_op *opline, *end;
opline = op_array->opcodes;
end = opline + op_array->last;
while (opline < end) {
if (opline->op1_type == IS_CONST) {
ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline->op1);
}
if (opline->op2_type == IS_CONST) {
ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline->op2);
}
opline++;
}
2015-12-24 11:30:41 +00:00
}
2015-12-24 11:30:41 +00:00
static void zend_redo_pass_two(zend_op_array *op_array)
{
zend_op *opline, *end;
2015-01-03 09:22:58 +00:00
opline = op_array->opcodes;
end = opline + op_array->last;
while (opline < end) {
if (opline->op1_type == IS_CONST) {
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
}
if (opline->op2_type == IS_CONST) {
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
}
ZEND_VM_SET_OPCODE_HANDLER(opline);
opline++;
}
}
2015-12-24 11:30:41 +00:00
static void zend_optimize_op_array(zend_op_array *op_array,
zend_optimizer_ctx *ctx)
{
/* Revert pass_two() */
zend_revert_pass_two(op_array);
/* Do actual optimizations */
zend_optimize(op_array, ctx);
/* Redo pass_two() */
zend_redo_pass_two(op_array);
}
static void zend_adjust_fcall_stack_size(zend_op_array *op_array, zend_optimizer_ctx *ctx)
{
zend_function *func;
zend_op *opline, *end;
opline = op_array->opcodes;
end = opline + op_array->last;
while (opline < end) {
if (opline->opcode == ZEND_INIT_FCALL) {
func = zend_hash_find_ptr(
&ctx->script->function_table,
Z_STR_P(RT_CONSTANT(op_array, opline->op2)));
if (func) {
opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, func);
}
}
opline++;
}
}
static void zend_adjust_fcall_stack_size_graph(zend_op_array *op_array)
{
zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
if (func_info) {
zend_call_info *call_info =func_info->callee_info;
while (call_info) {
zend_op *opline = call_info->caller_init_opline;
if (opline && call_info->callee_func) {
ZEND_ASSERT(opline->opcode == ZEND_INIT_FCALL);
opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, call_info->callee_func);
}
call_info = call_info->next_callee;
}
}
}
2015-12-09 11:52:00 +00:00
int zend_optimize_script(zend_script *script, zend_long optimization_level, zend_long debug_level)
{
uint idx, j;
Bucket *p, *q;
zend_class_entry *ce;
zend_op_array *op_array;
zend_optimizer_ctx ctx;
2015-12-24 11:30:41 +00:00
zend_call_graph call_graph;
ctx.arena = zend_arena_create(64 * 1024);
ctx.script = script;
ctx.constants = NULL;
2015-11-11 23:46:41 +00:00
ctx.optimization_level = optimization_level;
2015-12-09 11:52:00 +00:00
ctx.debug_level = debug_level;
zend_optimize_op_array(&script->main_op_array, &ctx);
for (idx = 0; idx < script->function_table.nNumUsed; idx++) {
p = script->function_table.arData + idx;
2014-03-28 19:34:49 +00:00
if (Z_TYPE(p->val) == IS_UNDEF) continue;
op_array = (zend_op_array*)Z_PTR(p->val);
zend_optimize_op_array(op_array, &ctx);
}
for (idx = 0; idx < script->class_table.nNumUsed; idx++) {
p = script->class_table.arData + idx;
2014-03-28 19:34:49 +00:00
if (Z_TYPE(p->val) == IS_UNDEF) continue;
ce = (zend_class_entry*)Z_PTR(p->val);
for (j = 0; j < ce->function_table.nNumUsed; j++) {
q = ce->function_table.arData + j;
2014-03-28 19:34:49 +00:00
if (Z_TYPE(q->val) == IS_UNDEF) continue;
op_array = (zend_op_array*)Z_PTR(q->val);
if (op_array->scope == ce) {
zend_optimize_op_array(op_array, &ctx);
} else if (op_array->type == ZEND_USER_FUNCTION) {
zend_op_array *orig_op_array;
2014-03-28 19:34:49 +00:00
if ((orig_op_array = zend_hash_find_ptr(&op_array->scope->function_table, q->key)) != NULL) {
HashTable *ht = op_array->static_variables;
*op_array = *orig_op_array;
op_array->static_variables = ht;
}
}
}
}
2015-12-24 11:30:41 +00:00
#if HAVE_DFA_PASS
if ((ZEND_OPTIMIZER_PASS_6 & optimization_level) &&
(ZEND_OPTIMIZER_PASS_7 & optimization_level) &&
zend_build_call_graph(&ctx.arena, script, ZEND_RT_CONSTANTS, &call_graph) == SUCCESS) {
/* Optimize using call-graph */
void *checkpoint = zend_arena_checkpoint(ctx.arena);
int i;
2015-12-24 11:30:41 +00:00
zend_func_info *func_info;
for (i = 0; i < call_graph.op_arrays_count; i++) {
zend_revert_pass_two(call_graph.op_arrays[i]);
}
for (i = 0; i < call_graph.op_arrays_count; i++) {
2015-12-24 11:30:41 +00:00
func_info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
if (func_info) {
zend_dfa_analyze_op_array(call_graph.op_arrays[i], &ctx, &func_info->ssa, &func_info->flags);
2015-12-24 11:30:41 +00:00
}
}
//TODO: perform inner-script inference???
2015-12-24 11:30:41 +00:00
for (i = 0; i < call_graph.op_arrays_count; i++) {
func_info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
if (func_info) {
zend_dfa_optimize_op_array(call_graph.op_arrays[i], &ctx, &func_info->ssa);
}
2015-12-24 11:30:41 +00:00
}
if (debug_level & ZEND_DUMP_AFTER_PASS_7) {
for (i = 0; i < call_graph.op_arrays_count; i++) {
zend_dump_op_array(call_graph.op_arrays[i], 0, "after pass 7", NULL);
}
}
if (ZEND_OPTIMIZER_PASS_12 & optimization_level) {
for (i = 0; i < call_graph.op_arrays_count; i++) {
zend_adjust_fcall_stack_size_graph(call_graph.op_arrays[i]);
}
}
2015-12-24 11:30:41 +00:00
for (i = 0; i < call_graph.op_arrays_count; i++) {
zend_redo_pass_two(call_graph.op_arrays[i]);
ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
}
zend_arena_release(&ctx.arena, checkpoint);
} else
2015-12-24 11:30:41 +00:00
#endif
if (ZEND_OPTIMIZER_PASS_12 & optimization_level) {
zend_adjust_fcall_stack_size(&script->main_op_array, &ctx);
for (idx = 0; idx < script->function_table.nNumUsed; idx++) {
p = script->function_table.arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
op_array = (zend_op_array*)Z_PTR(p->val);
zend_adjust_fcall_stack_size(op_array, &ctx);
}
for (idx = 0; idx < script->class_table.nNumUsed; idx++) {
p = script->class_table.arData + idx;
if (Z_TYPE(p->val) == IS_UNDEF) continue;
ce = (zend_class_entry*)Z_PTR(p->val);
for (j = 0; j < ce->function_table.nNumUsed; j++) {
q = ce->function_table.arData + j;
if (Z_TYPE(q->val) == IS_UNDEF) continue;
op_array = (zend_op_array*)Z_PTR(q->val);
if (op_array->scope == ce) {
zend_adjust_fcall_stack_size(op_array, &ctx);
} else if (op_array->type == ZEND_USER_FUNCTION) {
zend_op_array *orig_op_array;
if ((orig_op_array = zend_hash_find_ptr(&op_array->scope->function_table, q->key)) != NULL) {
HashTable *ht = op_array->static_variables;
*op_array = *orig_op_array;
op_array->static_variables = ht;
}
}
}
}
}
if (ctx.constants) {
zend_hash_destroy(ctx.constants);
}
zend_arena_destroy(ctx.arena);
return 1;
}
int zend_optimizer_startup(void)
{
return zend_func_info_startup();
}
int zend_optimizer_shutdown(void)
{
return zend_func_info_shutdown();
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/