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

230 lines
7.6 KiB
C
Raw Normal View History

2014-08-15 08:47:54 +00:00
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2016 The PHP Group |
2014-08-15 08:47:54 +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: 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"
#include "zend_bitset.h"
#define GET_AVAILABLE_T() \
for (i = 0; i < T; i++) { \
if (!zend_bitset_in(taken_T, i)) { \
break; \
} \
} \
zend_bitset_incl(taken_T, i); \
if (i > max) { \
max = i; \
2013-02-13 12:26:47 +00:00
}
2015-01-03 09:22:58 +00:00
void optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
int T = op_array->T;
2014-03-28 19:34:49 +00:00
int offset = op_array->last_var;
uint32_t bitset_len;
zend_bitset taken_T; /* T index in use */
2013-02-13 12:26:47 +00:00
zend_op **start_of_T; /* opline where T is first used */
zend_bitset valid_T; /* Is the map_T valid */
2013-02-13 12:26:47 +00:00
int *map_T; /* Map's the T to its new index */
zend_op *opline, *end;
int currT;
int i;
int max = -1;
int var_to_free = -1;
void *checkpoint = zend_arena_checkpoint(ctx->arena);
2013-02-13 12:26:47 +00:00
bitset_len = zend_bitset_len(T);
taken_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
start_of_T = (zend_op **) zend_arena_alloc(&ctx->arena, T * sizeof(zend_op *));
valid_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
map_T = (int *) zend_arena_alloc(&ctx->arena, T * sizeof(int));
2013-02-13 12:26:47 +00:00
end = op_array->opcodes;
2013-02-25 07:29:54 +00:00
opline = &op_array->opcodes[op_array->last - 1];
2013-02-13 12:26:47 +00:00
/* Find T definition points */
while (opline >= end) {
if (ZEND_RESULT_TYPE(opline) & (IS_VAR | IS_TMP_VAR)) {
2014-03-28 19:34:49 +00:00
start_of_T[VAR_NUM(ZEND_RESULT(opline).var) - offset] = opline;
2013-02-13 12:26:47 +00:00
}
opline--;
}
zend_bitset_clear(valid_T, bitset_len);
zend_bitset_clear(taken_T, bitset_len);
2013-02-13 12:26:47 +00:00
end = op_array->opcodes;
2013-02-25 07:29:54 +00:00
opline = &op_array->opcodes[op_array->last - 1];
2013-02-13 12:26:47 +00:00
while (opline >= end) {
2014-08-15 08:40:07 +00:00
if ((ZEND_OP1_TYPE(opline) & (IS_VAR | IS_TMP_VAR))) {
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
currT = VAR_NUM(ZEND_OP1(opline).var) - offset;
if (opline->opcode == ZEND_ROPE_END) {
int num = (((opline->extended_value + 1) * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
int var;
var = max;
while (var >= 0 && !zend_bitset_in(taken_T, var)) {
var--;
}
max = MAX(max, var + num);
var = var + 1;
map_T[currT] = var;
zend_bitset_incl(valid_T, currT);
zend_bitset_incl(taken_T, var);
ZEND_OP1(opline).var = NUM_VAR(var + offset);
while (num > 1) {
num--;
zend_bitset_incl(taken_T, var + num);
}
} else {
if (!zend_bitset_in(valid_T, currT)) {
int use_new_var = 0;
/* Code in "finally" blocks may modify temorary variables.
* We allocate new temporaries for values that need to
* relive FAST_CALLs.
*/
if ((op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) &&
(opline->opcode == ZEND_RETURN ||
opline->opcode == ZEND_RETURN_BY_REF ||
opline->opcode == ZEND_FREE ||
opline->opcode == ZEND_FE_FREE)) {
zend_op *curr = opline;
while (--curr >= end) {
if (curr->opcode == ZEND_FAST_CALL) {
use_new_var = 1;
break;
} else if (curr->opcode != ZEND_FREE &&
curr->opcode != ZEND_FE_FREE &&
curr->opcode != ZEND_VERIFY_RETURN_TYPE &&
curr->opcode != ZEND_DISCARD_EXCEPTION) {
break;
}
}
}
if (use_new_var) {
i = ++max;
zend_bitset_incl(taken_T, i);
} else {
GET_AVAILABLE_T();
}
map_T[currT] = i;
zend_bitset_incl(valid_T, currT);
}
ZEND_OP1(opline).var = NUM_VAR(map_T[currT] + offset);
2013-02-13 12:26:47 +00:00
}
}
/* Skip OP_DATA */
2013-02-22 06:56:05 +00:00
if (opline->opcode == ZEND_OP_DATA &&
2013-02-13 12:26:47 +00:00
(opline-1)->opcode == ZEND_ASSIGN_DIM) {
opline--;
continue;
}
2014-08-15 08:40:07 +00:00
if ((ZEND_OP2_TYPE(opline) & (IS_VAR | IS_TMP_VAR))) {
2014-03-28 19:34:49 +00:00
currT = VAR_NUM(ZEND_OP2(opline).var) - offset;
if (!zend_bitset_in(valid_T, currT)) {
2013-02-13 12:26:47 +00:00
GET_AVAILABLE_T();
map_T[currT] = i;
zend_bitset_incl(valid_T, currT);
2013-02-13 12:26:47 +00:00
}
2014-03-28 19:34:49 +00:00
ZEND_OP2(opline).var = NUM_VAR(map_T[currT] + offset);
2013-02-13 12:26:47 +00:00
}
if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS ||
2015-04-27 09:24:39 +00:00
opline->opcode == ZEND_DECLARE_ANON_INHERITED_CLASS ||
2013-02-13 12:26:47 +00:00
opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
2014-03-28 19:34:49 +00:00
currT = VAR_NUM(opline->extended_value) - offset;
if (!zend_bitset_in(valid_T, currT)) {
2013-02-13 12:26:47 +00:00
GET_AVAILABLE_T();
map_T[currT] = i;
zend_bitset_incl(valid_T, currT);
2013-02-13 12:26:47 +00:00
}
2014-03-28 19:34:49 +00:00
opline->extended_value = NUM_VAR(map_T[currT] + offset);
2013-02-13 12:26:47 +00:00
}
2013-03-11 18:49:05 +00:00
/* Allocate OP_DATA->op2 after "operands", but before "result" */
2013-02-13 12:26:47 +00:00
if (opline->opcode == ZEND_ASSIGN_DIM &&
2013-02-25 07:29:54 +00:00
(opline + 1)->opcode == ZEND_OP_DATA &&
ZEND_OP2_TYPE(opline + 1) & (IS_VAR | IS_TMP_VAR)) {
2014-03-28 19:34:49 +00:00
currT = VAR_NUM(ZEND_OP2(opline + 1).var) - offset;
2013-02-13 12:26:47 +00:00
GET_AVAILABLE_T();
map_T[currT] = i;
zend_bitset_incl(valid_T, currT);
zend_bitset_excl(taken_T, i);
2014-03-28 19:34:49 +00:00
ZEND_OP2(opline + 1).var = NUM_VAR(i + offset);
2013-02-13 12:26:47 +00:00
var_to_free = i;
}
if (ZEND_RESULT_TYPE(opline) & (IS_VAR | IS_TMP_VAR)) {
2014-08-15 08:40:07 +00:00
currT = VAR_NUM(ZEND_RESULT(opline).var) - offset;
if (zend_bitset_in(valid_T, currT)) {
2014-08-15 08:40:07 +00:00
if (start_of_T[currT] == opline) {
/* ZEND_FAST_CALL can not share temporary var with others
* since the fast_var could also be set by ZEND_HANDLE_EXCEPTION
* which could be ahead of it */
if (opline->opcode != ZEND_FAST_CALL) {
zend_bitset_excl(taken_T, map_T[currT]);
}
2014-08-15 08:40:07 +00:00
}
ZEND_RESULT(opline).var = NUM_VAR(map_T[currT] + offset);
if (opline->opcode == ZEND_ROPE_INIT) {
if (start_of_T[currT] == opline) {
uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
while (num > 1) {
num--;
zend_bitset_excl(taken_T, map_T[currT]+num);
}
}
}
2014-08-15 08:40:07 +00:00
} else { /* Au still needs to be assigned a T which is a bit dumb. Should consider changing Zend */
GET_AVAILABLE_T();
2013-02-13 12:26:47 +00:00
2014-08-15 08:40:07 +00:00
if (RESULT_UNUSED(opline)) {
zend_bitset_excl(taken_T, i);
2014-08-15 08:40:07 +00:00
} else {
/* Code which gets here is using a wrongly built opcode such as RECV() */
map_T[currT] = i;
zend_bitset_incl(valid_T, currT);
2013-02-13 12:26:47 +00:00
}
2014-08-15 08:40:07 +00:00
ZEND_RESULT(opline).var = NUM_VAR(i + offset);
2013-02-13 12:26:47 +00:00
}
}
if (var_to_free >= 0) {
zend_bitset_excl(taken_T, var_to_free);
2013-02-13 12:26:47 +00:00
var_to_free = -1;
}
opline--;
}
zend_arena_release(&ctx->arena, checkpoint);
2013-02-22 06:56:05 +00:00
op_array->T = max + 1;
2013-02-13 12:26:47 +00:00
}