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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@zend.com>                             |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_OPTIMIZER_INTERNAL_H
23 #define ZEND_OPTIMIZER_INTERNAL_H
24 
25 #include "zend_ssa.h"
26 
27 #define ZEND_RESULT_TYPE(opline)		(opline)->result_type
28 #define ZEND_RESULT(opline)				(opline)->result
29 #define ZEND_OP1_TYPE(opline)			(opline)->op1_type
30 #define ZEND_OP1(opline)				(opline)->op1
31 #define ZEND_OP1_LITERAL(opline)		(op_array)->literals[(opline)->op1.constant]
32 #define ZEND_OP1_JMP_ADDR(opline)		OP_JMP_ADDR(opline, (opline)->op1)
33 #define ZEND_OP2_TYPE(opline)			(opline)->op2_type
34 #define ZEND_OP2(opline)				(opline)->op2
35 #define ZEND_OP2_LITERAL(opline)		(op_array)->literals[(opline)->op2.constant]
36 #define ZEND_OP2_JMP_ADDR(opline)		OP_JMP_ADDR(opline, (opline)->op2)
37 
38 #define VAR_NUM(v) EX_VAR_TO_NUM(v)
39 #define NUM_VAR(v) ((uint32_t)(zend_uintptr_t)ZEND_CALL_VAR_NUM(0, v))
40 
41 #define INV_COND(op)       ((op) == ZEND_JMPZ    ? ZEND_JMPNZ    : ZEND_JMPZ)
42 #define INV_EX_COND(op)    ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ    : ZEND_JMPZ)
43 #define INV_COND_EX(op)    ((op) == ZEND_JMPZ    ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
44 #define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
45 
46 #define RESULT_UNUSED(op)	(op->result_type == IS_UNUSED)
47 #define SAME_VAR(op1, op2)  (op1 ## _type == op2 ## _type && op1.var == op2.var)
48 
49 typedef struct _zend_optimizer_ctx {
50 	zend_arena             *arena;
51 	zend_script            *script;
52 	HashTable              *constants;
53 	zend_long               optimization_level;
54 	zend_long               debug_level;
55 } zend_optimizer_ctx;
56 
57 #define LITERAL_LONG(op, val) do { \
58 		zval _c; \
59 		ZVAL_LONG(&_c, val); \
60 		op.constant = zend_optimizer_add_literal(op_array, &_c); \
61 	} while (0)
62 
63 #define LITERAL_BOOL(op, val) do { \
64 		zval _c; \
65 		ZVAL_BOOL(&_c, val); \
66 		op.constant = zend_optimizer_add_literal(op_array, &_c); \
67 	} while (0)
68 
69 #define literal_dtor(zv) do { \
70 		zval_dtor(zv); \
71 		ZVAL_NULL(zv); \
72 	} while (0)
73 
74 #define COPY_NODE(target, src) do { \
75 		target ## _type = src ## _type; \
76 		target = src; \
77 	} while (0)
78 
79 int  zend_optimizer_add_literal(zend_op_array *op_array, zval *zv);
80 int  zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy);
81 void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value);
82 int  zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value);
83 int  zend_optimizer_lookup_cv(zend_op_array *op_array, zend_string* name);
84 int zend_optimizer_update_op1_const(zend_op_array *op_array,
85                                     zend_op       *opline,
86                                     zval          *val);
87 int zend_optimizer_update_op2_const(zend_op_array *op_array,
88                                     zend_op       *opline,
89                                     zval          *val);
90 int  zend_optimizer_replace_by_const(zend_op_array *op_array,
91                                      zend_op       *opline,
92                                      zend_uchar     type,
93                                      uint32_t       var,
94                                      zval          *val);
95 
96 void zend_optimizer_remove_live_range(zend_op_array *op_array, uint32_t var);
97 void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx);
98 void zend_optimizer_pass2(zend_op_array *op_array);
99 void zend_optimizer_pass3(zend_op_array *op_array);
100 void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx);
101 void zend_optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx);
102 void zend_optimize_dfa(zend_op_array *op_array, zend_optimizer_ctx *ctx);
103 int  zend_dfa_analyze_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, uint32_t *flags);
104 void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa);
105 void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx);
106 void zend_optimizer_nop_removal(zend_op_array *op_array);
107 void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx *ctx);
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 
113 #endif
114