xref: /PHP-7.4/ext/opcache/Optimizer/nop_removal.c (revision da919a8b)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 /* pass 10:
23  * - remove NOPs
24  */
25 
26 #include "php.h"
27 #include "Optimizer/zend_optimizer.h"
28 #include "Optimizer/zend_optimizer_internal.h"
29 #include "zend_API.h"
30 #include "zend_constants.h"
31 #include "zend_execute.h"
32 #include "zend_vm.h"
33 
zend_optimizer_nop_removal(zend_op_array * op_array,zend_optimizer_ctx * ctx)34 void zend_optimizer_nop_removal(zend_op_array *op_array, zend_optimizer_ctx *ctx)
35 {
36 	zend_op *end, *opline;
37 	uint32_t new_count, i, shift;
38 	int j;
39 	uint32_t *shiftlist;
40 	ALLOCA_FLAG(use_heap);
41 
42 	shiftlist = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last, use_heap);
43 	i = new_count = shift = 0;
44 	end = op_array->opcodes + op_array->last;
45 	for (opline = op_array->opcodes; opline < end; opline++) {
46 
47 		/* Kill JMP-over-NOP-s */
48 		if (opline->opcode == ZEND_JMP && ZEND_OP1_JMP_ADDR(opline) > op_array->opcodes + i) {
49 			/* check if there are only NOPs under the branch */
50 			zend_op *target = ZEND_OP1_JMP_ADDR(opline) - 1;
51 
52 			while (target->opcode == ZEND_NOP) {
53 				target--;
54 			}
55 			if (target == opline) {
56 				/* only NOPs */
57 				opline->opcode = ZEND_NOP;
58 			}
59 		}
60 
61 		shiftlist[i++] = shift;
62 		if (opline->opcode == ZEND_NOP) {
63 			shift++;
64 		} else {
65 			if (shift) {
66 				zend_op *new_opline = op_array->opcodes + new_count;
67 
68 				*new_opline = *opline;
69 				zend_optimizer_migrate_jump(op_array, new_opline, opline);
70 			}
71 			new_count++;
72 		}
73 	}
74 
75 	if (shift) {
76 		op_array->last = new_count;
77 		end = op_array->opcodes + op_array->last;
78 
79 		/* update JMPs */
80 		for (opline = op_array->opcodes; opline<end; opline++) {
81 			zend_optimizer_shift_jump(op_array, opline, shiftlist);
82 		}
83 
84 		/* update try/catch array */
85 		for (j = 0; j < op_array->last_try_catch; j++) {
86 			op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op];
87 			op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op];
88 			if (op_array->try_catch_array[j].finally_op) {
89 				op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op];
90 				op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end];
91 			}
92 		}
93 
94 		/* update early binding list */
95 		if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
96 			uint32_t *opline_num = &ctx->script->first_early_binding_opline;
97 
98 			ZEND_ASSERT(op_array == &ctx->script->main_op_array);
99 			do {
100 				*opline_num -= shiftlist[*opline_num];
101 				opline_num = &op_array->opcodes[*opline_num].result.opline_num;
102 			} while (*opline_num != (uint32_t)-1);
103 		}
104 	}
105 	free_alloca(shiftlist, use_heap);
106 }
107