xref: /php-src/Zend/Optimizer/nop_removal.c (revision c19977d0)
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    | https://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 "Optimizer/zend_optimizer.h"
27 #include "Optimizer/zend_optimizer_internal.h"
28 #include "zend_API.h"
29 #include "zend_constants.h"
30 #include "zend_execute.h"
31 #include "zend_vm.h"
32 
zend_optimizer_nop_removal(zend_op_array * op_array,zend_optimizer_ctx * ctx)33 void zend_optimizer_nop_removal(zend_op_array *op_array, zend_optimizer_ctx *ctx)
34 {
35 	zend_op *end, *opline;
36 	uint32_t new_count, i, shift;
37 	int j;
38 	uint32_t *shiftlist;
39 	ALLOCA_FLAG(use_heap);
40 
41 	shiftlist = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last, use_heap);
42 	i = new_count = shift = 0;
43 	end = op_array->opcodes + op_array->last;
44 	for (opline = op_array->opcodes; opline < end; opline++) {
45 
46 		/* Kill JMP-over-NOP-s */
47 		if (opline->opcode == ZEND_JMP && ZEND_OP1_JMP_ADDR(opline) > op_array->opcodes + i) {
48 			/* check if there are only NOPs under the branch */
49 			zend_op *target = ZEND_OP1_JMP_ADDR(opline) - 1;
50 
51 			while (target->opcode == ZEND_NOP) {
52 				target--;
53 			}
54 			if (target == opline) {
55 				/* only NOPs */
56 				opline->opcode = ZEND_NOP;
57 			}
58 		}
59 
60 		shiftlist[i++] = shift;
61 		if (opline->opcode == ZEND_NOP) {
62 			shift++;
63 		} else {
64 			if (shift) {
65 				zend_op *new_opline = op_array->opcodes + new_count;
66 
67 				*new_opline = *opline;
68 				zend_optimizer_migrate_jump(op_array, new_opline, opline);
69 			}
70 			new_count++;
71 		}
72 	}
73 
74 	if (shift) {
75 		op_array->last = new_count;
76 		end = op_array->opcodes + op_array->last;
77 
78 		/* update JMPs */
79 		for (opline = op_array->opcodes; opline<end; opline++) {
80 			zend_optimizer_shift_jump(op_array, opline, shiftlist);
81 		}
82 
83 		/* update try/catch array */
84 		for (j = 0; j < op_array->last_try_catch; j++) {
85 			op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op];
86 			op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op];
87 			if (op_array->try_catch_array[j].finally_op) {
88 				op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op];
89 				op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end];
90 			}
91 		}
92 	}
93 	free_alloca(shiftlist, use_heap);
94 }
95