2015-12-11 14:24:55 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine, DFG - Data Flow Graph |
|
|
|
|
+----------------------------------------------------------------------+
|
2016-01-01 18:06:12 +00:00
|
|
|
| Copyright (c) 1998-2016 The PHP Group |
|
2015-12-11 14:24:55 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| 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: Dmitry Stogov <dmitry@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "zend_compile.h"
|
|
|
|
#include "zend_dfg.h"
|
|
|
|
|
2016-01-23 15:56:27 +00:00
|
|
|
int zend_build_dfg(const zend_op_array *op_array, const zend_cfg *cfg, zend_dfg *dfg, uint32_t build_flags) /* {{{ */
|
2015-12-11 14:24:55 +00:00
|
|
|
{
|
|
|
|
int set_size;
|
|
|
|
zend_basic_block *blocks = cfg->blocks;
|
|
|
|
int blocks_count = cfg->blocks_count;
|
2016-04-21 21:12:28 +00:00
|
|
|
zend_bitset tmp, def, use, in, out;
|
2015-12-11 14:24:55 +00:00
|
|
|
zend_op *opline;
|
2016-03-31 14:26:27 +00:00
|
|
|
uint32_t k, var_num;
|
2016-01-09 12:28:16 +00:00
|
|
|
int j;
|
2015-12-11 14:24:55 +00:00
|
|
|
|
|
|
|
set_size = dfg->size;
|
|
|
|
tmp = dfg->tmp;
|
|
|
|
def = dfg->def;
|
|
|
|
use = dfg->use;
|
|
|
|
in = dfg->in;
|
|
|
|
out = dfg->out;
|
|
|
|
|
2016-04-21 21:12:28 +00:00
|
|
|
/* Collect "def" and "use" sets */
|
2015-12-11 14:24:55 +00:00
|
|
|
for (j = 0; j < blocks_count; j++) {
|
|
|
|
if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (k = blocks[j].start; k <= blocks[j].end; k++) {
|
|
|
|
opline = op_array->opcodes + k;
|
|
|
|
if (opline->opcode != ZEND_OP_DATA) {
|
|
|
|
zend_op *next = opline + 1;
|
|
|
|
if (k < blocks[j].end &&
|
|
|
|
next->opcode == ZEND_OP_DATA) {
|
|
|
|
if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(next->op1.var);
|
|
|
|
if (!DFG_ISSET(def, set_size, j, var_num)) {
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 16:58:36 +00:00
|
|
|
if (next->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(next->op2.var);
|
|
|
|
if (!DFG_ISSET(def, set_size, j, var_num)) {
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opline->op1_type == IS_CV) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(opline->op1.var);
|
2015-12-11 14:24:55 +00:00
|
|
|
switch (opline->opcode) {
|
2016-01-23 15:56:27 +00:00
|
|
|
case ZEND_ADD_ARRAY_ELEMENT:
|
|
|
|
case ZEND_INIT_ARRAY:
|
|
|
|
if ((build_flags & ZEND_SSA_RC_INFERENCE)
|
|
|
|
|| (opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
|
|
|
|
goto op1_def;
|
|
|
|
}
|
|
|
|
goto op1_use;
|
|
|
|
case ZEND_FE_RESET_R:
|
|
|
|
if (build_flags & ZEND_SSA_RC_INFERENCE) {
|
|
|
|
goto op1_def;
|
|
|
|
}
|
|
|
|
goto op1_use;
|
|
|
|
case ZEND_YIELD:
|
|
|
|
if ((build_flags & ZEND_SSA_RC_INFERENCE)
|
|
|
|
|| (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
|
|
|
|
goto op1_def;
|
|
|
|
}
|
|
|
|
goto op1_use;
|
2016-04-21 21:12:28 +00:00
|
|
|
case ZEND_UNSET_VAR:
|
|
|
|
ZEND_ASSERT(opline->extended_value & ZEND_QUICK_SET);
|
|
|
|
/* break missing intentionally */
|
2015-12-11 14:24:55 +00:00
|
|
|
case ZEND_ASSIGN:
|
|
|
|
case ZEND_ASSIGN_REF:
|
|
|
|
case ZEND_BIND_GLOBAL:
|
2016-01-12 09:20:35 +00:00
|
|
|
case ZEND_BIND_STATIC:
|
2015-12-11 14:24:55 +00:00
|
|
|
case ZEND_SEND_VAR_EX:
|
|
|
|
case ZEND_SEND_REF:
|
|
|
|
case ZEND_SEND_VAR_NO_REF:
|
|
|
|
case ZEND_FE_RESET_RW:
|
|
|
|
case ZEND_ASSIGN_ADD:
|
|
|
|
case ZEND_ASSIGN_SUB:
|
|
|
|
case ZEND_ASSIGN_MUL:
|
|
|
|
case ZEND_ASSIGN_DIV:
|
|
|
|
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:
|
|
|
|
case ZEND_ASSIGN_POW:
|
|
|
|
case ZEND_PRE_INC:
|
|
|
|
case ZEND_PRE_DEC:
|
|
|
|
case ZEND_POST_INC:
|
|
|
|
case ZEND_POST_DEC:
|
|
|
|
case ZEND_ASSIGN_DIM:
|
|
|
|
case ZEND_ASSIGN_OBJ:
|
|
|
|
case ZEND_UNSET_DIM:
|
|
|
|
case ZEND_UNSET_OBJ:
|
|
|
|
case ZEND_FETCH_DIM_W:
|
|
|
|
case ZEND_FETCH_DIM_RW:
|
|
|
|
case ZEND_FETCH_DIM_FUNC_ARG:
|
|
|
|
case ZEND_FETCH_DIM_UNSET:
|
|
|
|
case ZEND_FETCH_OBJ_W:
|
|
|
|
case ZEND_FETCH_OBJ_RW:
|
|
|
|
case ZEND_FETCH_OBJ_FUNC_ARG:
|
|
|
|
case ZEND_FETCH_OBJ_UNSET:
|
2016-04-07 14:34:53 +00:00
|
|
|
case ZEND_VERIFY_RETURN_TYPE:
|
2016-04-21 21:12:28 +00:00
|
|
|
op1_def:
|
|
|
|
// FIXME: include into "use" too ...?
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
|
|
|
DFG_SET(def, set_size, j, var_num);
|
|
|
|
break;
|
2015-12-11 14:24:55 +00:00
|
|
|
default:
|
2016-01-23 15:56:27 +00:00
|
|
|
op1_use:
|
2016-03-31 14:26:27 +00:00
|
|
|
if (!DFG_ISSET(def, set_size, j, var_num)) {
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 20:11:19 +00:00
|
|
|
} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(opline->op1.var);
|
2016-04-07 14:34:53 +00:00
|
|
|
if (opline->opcode == ZEND_VERIFY_RETURN_TYPE) {
|
2016-04-21 21:12:28 +00:00
|
|
|
DFG_SET(use, set_size, j, var_num);
|
|
|
|
DFG_SET(def, set_size, j, var_num);
|
|
|
|
} else if (!DFG_ISSET(def, set_size, j, var_num)) {
|
2016-03-31 14:26:27 +00:00
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opline->op2_type == IS_CV) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(opline->op2.var);
|
2015-12-11 14:24:55 +00:00
|
|
|
switch (opline->opcode) {
|
|
|
|
case ZEND_ASSIGN:
|
2016-01-23 15:56:27 +00:00
|
|
|
if (build_flags & ZEND_SSA_RC_INFERENCE) {
|
|
|
|
goto op2_def;
|
|
|
|
}
|
|
|
|
goto op2_use;
|
|
|
|
case ZEND_BIND_LEXICAL:
|
|
|
|
if ((build_flags & ZEND_SSA_RC_INFERENCE) || opline->extended_value) {
|
|
|
|
goto op2_def;
|
|
|
|
}
|
|
|
|
goto op2_use;
|
2015-12-11 14:24:55 +00:00
|
|
|
case ZEND_ASSIGN_REF:
|
|
|
|
case ZEND_FE_FETCH_R:
|
|
|
|
case ZEND_FE_FETCH_RW:
|
2016-01-23 15:56:27 +00:00
|
|
|
op2_def:
|
2016-04-21 21:12:28 +00:00
|
|
|
// FIXME: include into "use" too ...?
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
|
|
|
DFG_SET(def, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-01-23 15:56:27 +00:00
|
|
|
op2_use:
|
2016-03-31 14:26:27 +00:00
|
|
|
if (!DFG_ISSET(def, set_size, j, var_num)) {
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-02-11 20:11:19 +00:00
|
|
|
} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
|
2016-03-31 14:26:27 +00:00
|
|
|
var_num = EX_VAR_TO_NUM(opline->op2.var);
|
2015-12-11 14:24:55 +00:00
|
|
|
if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
|
2016-04-21 21:12:28 +00:00
|
|
|
DFG_SET(def, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
} else {
|
2016-03-31 14:26:27 +00:00
|
|
|
if (!DFG_ISSET(def, set_size, j, var_num)) {
|
|
|
|
DFG_SET(use, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 14:26:27 +00:00
|
|
|
if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
|
|
|
|
var_num = EX_VAR_TO_NUM(opline->result.var);
|
2016-04-21 21:12:28 +00:00
|
|
|
DFG_SET(def, set_size, j, var_num);
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate "in" and "out" sets */
|
2016-01-09 12:28:16 +00:00
|
|
|
{
|
|
|
|
uint32_t worklist_len = zend_bitset_len(blocks_count);
|
2016-03-17 02:53:01 +00:00
|
|
|
zend_bitset worklist;
|
2016-01-09 12:28:16 +00:00
|
|
|
ALLOCA_FLAG(use_heap);
|
2016-03-17 02:53:01 +00:00
|
|
|
worklist = ZEND_BITSET_ALLOCA(worklist_len, use_heap);
|
2016-01-09 12:28:16 +00:00
|
|
|
memset(worklist, 0, worklist_len * ZEND_BITSET_ELM_SIZE);
|
2015-12-11 14:24:55 +00:00
|
|
|
for (j = 0; j < blocks_count; j++) {
|
2016-01-09 12:28:16 +00:00
|
|
|
zend_bitset_incl(worklist, j);
|
|
|
|
}
|
|
|
|
while (!zend_bitset_empty(worklist, worklist_len)) {
|
|
|
|
/* We use the last block on the worklist, because predecessors tend to be located
|
|
|
|
* before the succeeding block, so this converges faster. */
|
|
|
|
j = zend_bitset_last(worklist, worklist_len);
|
|
|
|
zend_bitset_excl(worklist, j);
|
|
|
|
|
2015-12-11 14:24:55 +00:00
|
|
|
if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (blocks[j].successors[0] >= 0) {
|
|
|
|
zend_bitset_copy(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[0]), set_size);
|
|
|
|
if (blocks[j].successors[1] >= 0) {
|
|
|
|
zend_bitset_union(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[1]), set_size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
zend_bitset_clear(DFG_BITSET(out, set_size, j), set_size);
|
|
|
|
}
|
|
|
|
zend_bitset_union_with_difference(tmp, DFG_BITSET(use, set_size, j), DFG_BITSET(out, set_size, j), DFG_BITSET(def, set_size, j), set_size);
|
|
|
|
if (!zend_bitset_equal(DFG_BITSET(in, set_size, j), tmp, set_size)) {
|
|
|
|
zend_bitset_copy(DFG_BITSET(in, set_size, j), tmp, set_size);
|
2016-01-09 12:28:16 +00:00
|
|
|
|
|
|
|
/* Add predecessors of changed block to worklist */
|
|
|
|
{
|
|
|
|
int *predecessors = &cfg->predecessors[blocks[j].predecessor_offset];
|
|
|
|
for (k = 0; k < blocks[j].predecessors_count; k++) {
|
|
|
|
zend_bitset_incl(worklist, predecessors[k]);
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 14:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 12:28:16 +00:00
|
|
|
|
|
|
|
free_alloca(worklist, use_heap);
|
|
|
|
}
|
2015-12-11 14:24:55 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|