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

241 lines
7.5 KiB
C
Raw Normal View History

2014-08-15 08:47:54 +00:00
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
2015-01-15 15:27:30 +00:00
| Copyright (c) 1998-2015 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> |
+----------------------------------------------------------------------+
*/
2013-02-13 12:26:47 +00:00
/* pass 2:
* - convert non-numeric constants to numeric constants in numeric operators
* - optimize constant conditional JMPs
* - optimize static BRKs and CONTs
*/
#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"
2014-12-13 22:06:14 +00:00
void zend_optimizer_pass2(zend_op_array *op_array)
{
2013-02-13 12:26:47 +00:00
zend_op *opline;
2013-02-22 06:56:05 +00:00
zend_op *end = op_array->opcodes + op_array->last;
2013-02-13 12:26:47 +00:00
opline = op_array->opcodes;
2013-02-22 06:56:05 +00:00
while (opline < end) {
2013-02-13 12:26:47 +00:00
switch (opline->opcode) {
case ZEND_ADD:
case ZEND_SUB:
case ZEND_MUL:
case ZEND_DIV:
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
if (Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING) {
2014-12-13 22:06:14 +00:00
convert_scalar_to_number(&ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
}
}
/* break missing *intentionally* - the assign_op's may only optimize op2 */
case ZEND_ASSIGN_ADD:
case ZEND_ASSIGN_SUB:
case ZEND_ASSIGN_MUL:
case ZEND_ASSIGN_DIV:
2013-02-22 06:56:05 +00:00
if (opline->extended_value != 0) {
2013-02-13 12:26:47 +00:00
/* object tristate op - don't attempt to optimize it! */
break;
}
if (ZEND_OP2_TYPE(opline) == IS_CONST) {
if (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) {
2014-12-13 22:06:14 +00:00
convert_scalar_to_number(&ZEND_OP2_LITERAL(opline));
2013-02-13 12:26:47 +00:00
}
}
break;
case ZEND_MOD:
case ZEND_SL:
case ZEND_SR:
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
2014-08-25 17:24:55 +00:00
if (Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_LONG) {
2014-08-25 19:51:49 +00:00
convert_to_long(&ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
}
}
/* break missing *intentionally - the assign_op's may only optimize op2 */
case ZEND_ASSIGN_MOD:
case ZEND_ASSIGN_SL:
case ZEND_ASSIGN_SR:
2013-02-22 06:56:05 +00:00
if (opline->extended_value != 0) {
2013-02-13 12:26:47 +00:00
/* object tristate op - don't attempt to optimize it! */
break;
}
if (ZEND_OP2_TYPE(opline) == IS_CONST) {
2014-08-25 17:24:55 +00:00
if (Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_LONG) {
2014-08-25 19:51:49 +00:00
convert_to_long(&ZEND_OP2_LITERAL(opline));
2013-02-13 12:26:47 +00:00
}
}
break;
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
case ZEND_CONCAT:
case ZEND_FAST_CONCAT:
2013-02-13 12:26:47 +00:00
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
if (Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string(&ZEND_OP1_LITERAL(opline));
}
}
/* break missing *intentionally - the assign_op's may only optimize op2 */
case ZEND_ASSIGN_CONCAT:
2013-02-22 06:56:05 +00:00
if (opline->extended_value != 0) {
2013-02-13 12:26:47 +00:00
/* object tristate op - don't attempt to optimize it! */
break;
}
if (ZEND_OP2_TYPE(opline) == IS_CONST) {
if (Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string(&ZEND_OP2_LITERAL(opline));
}
}
break;
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
/* convert Ti = JMPZ_EX(Ti, L) to JMPZ(Ti, L) */
2013-03-11 18:49:05 +00:00
if (0 && /* FIXME: temporary disable unsafe pattern */
2013-02-14 09:06:30 +00:00
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
2013-02-13 12:26:47 +00:00
ZEND_RESULT_TYPE(opline) == IS_TMP_VAR &&
ZEND_OP1(opline).var == ZEND_RESULT(opline).var) {
opline->opcode -= 3;
/* convert Ti = JMPZ_EX(C, L) => Ti = QM_ASSIGN(C)
in case we know it wouldn't jump */
} else if (ZEND_OP1_TYPE(opline) == IS_CONST) {
2014-12-13 22:06:14 +00:00
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
if (opline->opcode == ZEND_JMPZ_EX) {
should_jmp = !should_jmp;
}
if (!should_jmp) {
opline->opcode = ZEND_QM_ASSIGN;
SET_UNUSED(opline->op2);
}
}
break;
case ZEND_JMPZ:
case ZEND_JMPNZ:
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
2014-12-13 22:06:14 +00:00
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
if (opline->opcode == ZEND_JMPZ) {
should_jmp = !should_jmp;
}
literal_dtor(&ZEND_OP1_LITERAL(opline));
ZEND_OP1_TYPE(opline) = IS_UNUSED;
if (should_jmp) {
opline->opcode = ZEND_JMP;
COPY_NODE(opline->op1, opline->op2);
} else {
MAKE_NOP(opline);
}
break;
}
2013-02-22 06:56:05 +00:00
if ((opline + 1)->opcode == ZEND_JMP) {
2013-02-13 12:26:47 +00:00
/* JMPZ(X, L1), JMP(L2) => JMPZNZ(X, L1, L2) */
/* JMPNZ(X, L1), JMP(L2) => JMPZNZ(X, L2, L1) */
2013-02-22 06:56:05 +00:00
if (ZEND_OP2(opline).opline_num == ZEND_OP1(opline + 1).opline_num) {
2013-02-13 12:26:47 +00:00
/* JMPZ(X, L1), JMP(L1) => NOP, JMP(L1) */
MAKE_NOP(opline);
} else {
if (opline->opcode == ZEND_JMPZ) {
2013-02-22 06:56:05 +00:00
opline->extended_value = ZEND_OP1(opline + 1).opline_num;
2013-02-13 12:26:47 +00:00
} else {
opline->extended_value = ZEND_OP2(opline).opline_num;
2013-02-22 06:56:05 +00:00
COPY_NODE(opline->op2, (opline + 1)->op1);
2013-02-13 12:26:47 +00:00
}
opline->opcode = ZEND_JMPZNZ;
}
}
break;
case ZEND_JMPZNZ:
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
int opline_num;
2014-12-13 22:06:14 +00:00
if (zend_is_true(&ZEND_OP1_LITERAL(opline))) {
2013-02-13 12:26:47 +00:00
opline_num = opline->extended_value; /* JMPNZ */
} else {
opline_num = ZEND_OP2(opline).opline_num; /* JMPZ */
}
literal_dtor(&ZEND_OP1_LITERAL(opline));
ZEND_OP1(opline).opline_num = opline_num;
ZEND_OP1_TYPE(opline) = IS_UNUSED;
opline->opcode = ZEND_JMP;
}
break;
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
case ZEND_BRK:
case ZEND_CONT:
{
zend_brk_cont_element *jmp_to;
int array_offset;
int nest_levels;
2013-02-22 06:56:05 +00:00
int dont_optimize = 0;
2013-02-13 12:26:47 +00:00
ZEND_ASSERT(ZEND_OP2_TYPE(opline) == IS_CONST);
ZEND_ASSERT(Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_LONG);
nest_levels = Z_LVAL(ZEND_OP2_LITERAL(opline));
2013-02-13 12:26:47 +00:00
array_offset = ZEND_OP1(opline).opline_num;
while (1) {
2013-02-22 06:56:05 +00:00
if (array_offset == -1) {
dont_optimize = 1; /* don't optimize this bogus break/continue, let the executor shout */
2013-02-13 12:26:47 +00:00
break;
}
jmp_to = &op_array->brk_cont_array[array_offset];
array_offset = jmp_to->parent;
if (--nest_levels > 0) {
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
if (op_array->opcodes[jmp_to->brk].opcode == ZEND_FREE ||
op_array->opcodes[jmp_to->brk].opcode == ZEND_FE_FREE ||
op_array->opcodes[jmp_to->brk].opcode == ZEND_END_SILENCE) {
2013-02-22 06:56:05 +00:00
dont_optimize = 1;
2013-02-13 12:26:47 +00:00
break;
}
} else {
break;
}
}
if (dont_optimize) {
break;
}
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
/* optimize - convert to a JMP */
switch (opline->opcode) {
case ZEND_BRK:
MAKE_NOP(opline);
ZEND_OP1(opline).opline_num = jmp_to->brk;
break;
case ZEND_CONT:
MAKE_NOP(opline);
ZEND_OP1(opline).opline_num = jmp_to->cont;
break;
}
opline->opcode = ZEND_JMP;
/* MAKE_NOP() already set op1 and op2 to IS_UNUSED */
}
break;
}
opline++;
}
}