1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_OPTIMIZER_INTERNAL_H
23 #define ZEND_OPTIMIZER_INTERNAL_H
24 
25 #include "zend_ssa.h"
26 #include "zend_func_info.h"
27 
28 #define ZEND_OP1_LITERAL(opline)		(op_array)->literals[(opline)->op1.constant]
29 #define ZEND_OP1_JMP_ADDR(opline)		OP_JMP_ADDR(opline, (opline)->op1)
30 #define ZEND_OP2_LITERAL(opline)		(op_array)->literals[(opline)->op2.constant]
31 #define ZEND_OP2_JMP_ADDR(opline)		OP_JMP_ADDR(opline, (opline)->op2)
32 
33 #define VAR_NUM(v) EX_VAR_TO_NUM(v)
34 #define NUM_VAR(v) ((uint32_t)(zend_uintptr_t)ZEND_CALL_VAR_NUM(0, v))
35 
36 #define INV_COND(op)       ((op) == ZEND_JMPZ    ? ZEND_JMPNZ    : ZEND_JMPZ)
37 #define INV_EX_COND(op)    ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ    : ZEND_JMPZ)
38 #define INV_COND_EX(op)    ((op) == ZEND_JMPZ    ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
39 #define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
40 
41 #define RESULT_UNUSED(op)	(op->result_type == IS_UNUSED)
42 #define SAME_VAR(op1, op2)  (op1 ## _type == op2 ## _type && op1.var == op2.var)
43 
44 typedef struct _zend_optimizer_ctx {
45 	zend_arena             *arena;
46 	zend_script            *script;
47 	HashTable              *constants;
48 	zend_long               optimization_level;
49 	zend_long               debug_level;
50 } zend_optimizer_ctx;
51 
52 #define LITERAL_LONG(op, val) do { \
53 		zval _c; \
54 		ZVAL_LONG(&_c, val); \
55 		op.constant = zend_optimizer_add_literal(op_array, &_c); \
56 	} while (0)
57 
58 #define LITERAL_BOOL(op, val) do { \
59 		zval _c; \
60 		ZVAL_BOOL(&_c, val); \
61 		op.constant = zend_optimizer_add_literal(op_array, &_c); \
62 	} while (0)
63 
64 #define literal_dtor(zv) do { \
65 		zval_ptr_dtor_nogc(zv); \
66 		ZVAL_NULL(zv); \
67 	} while (0)
68 
69 #define COPY_NODE(target, src) do { \
70 		target ## _type = src ## _type; \
71 		target = src; \
72 	} while (0)
73 
74 int  zend_optimizer_add_literal(zend_op_array *op_array, zval *zv);
75 int  zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy);
76 void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value);
77 int  zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value);
78 int zend_optimizer_eval_binary_op(zval *result, zend_uchar opcode, zval *op1, zval *op2);
79 int zend_optimizer_eval_unary_op(zval *result, zend_uchar opcode, zval *op1);
80 int zend_optimizer_eval_cast(zval *result, uint32_t type, zval *op1);
81 int zend_optimizer_eval_strlen(zval *result, zval *op1);
82 int zend_optimizer_update_op1_const(zend_op_array *op_array,
83                                     zend_op       *opline,
84                                     zval          *val);
85 int zend_optimizer_update_op2_const(zend_op_array *op_array,
86                                     zend_op       *opline,
87                                     zval          *val);
88 int  zend_optimizer_replace_by_const(zend_op_array *op_array,
89                                      zend_op       *opline,
90                                      zend_uchar     type,
91                                      uint32_t       var,
92                                      zval          *val);
93 
94 void zend_optimizer_remove_live_range(zend_op_array *op_array, uint32_t var);
95 void zend_optimizer_remove_live_range_ex(zend_op_array *op_array, uint32_t var, uint32_t start);
96 void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx);
97 void zend_optimizer_pass2(zend_op_array *op_array);
98 void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx);
99 void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx);
100 void zend_optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx);
101 void zend_optimize_dfa(zend_op_array *op_array, zend_optimizer_ctx *ctx);
102 int  zend_dfa_analyze_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa);
103 void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map);
104 void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx);
105 void zend_optimizer_nop_removal(zend_op_array *op_array, zend_optimizer_ctx *ctx);
106 void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx *ctx);
107 void zend_optimizer_compact_vars(zend_op_array *op_array);
108 int zend_optimizer_is_disabled_func(const char *name, size_t len);
109 zend_function *zend_optimizer_get_called_func(
110 		zend_script *script, zend_op_array *op_array, zend_op *opline, zend_bool rt_constants);
111 uint32_t zend_optimizer_classify_function(zend_string *name, uint32_t num_args);
112 void zend_optimizer_migrate_jump(zend_op_array *op_array, zend_op *new_opline, zend_op *opline);
113 void zend_optimizer_shift_jump(zend_op_array *op_array, zend_op *opline, uint32_t *shiftlist);
114 zend_uchar zend_compound_assign_to_binary_op(zend_uchar opcode);
115 int sccp_optimize_op_array(zend_optimizer_ctx *ctx, zend_op_array *op_arrya, zend_ssa *ssa, zend_call_info **call_map);
116 int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, zend_bool reorder_dtor_effects);
117 int zend_ssa_escape_analysis(const zend_script *script, zend_op_array *op_array, zend_ssa *ssa);
118 
119 #endif
120