Coverage Report

Created: 2026-10-28 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/Optimizer/pass3.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend OPcache                                                         |
4
   +----------------------------------------------------------------------+
5
   | Copyright © The PHP Group and Contributors.                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to the Modified BSD License that is      |
8
   | bundled with this package in the file LICENSE, and is available      |
9
   | through the World Wide Web at <https://www.php.net/license/>.        |
10
   |                                                                      |
11
   | SPDX-License-Identifier: BSD-3-Clause                                |
12
   +----------------------------------------------------------------------+
13
   | Authors: Andi Gutmans <andi@php.net>                                 |
14
   |          Zeev Suraski <zeev@php.net>                                 |
15
   |          Stanislav Malyshev <stas@zend.com>                          |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
/* pass 3: (Jump optimization)
21
 * - optimize series of JMPs
22
 */
23
24
#include "Optimizer/zend_optimizer.h"
25
#include "Optimizer/zend_optimizer_internal.h"
26
#include "zend_API.h"
27
#include "zend_constants.h"
28
#include "zend_execute.h"
29
#include "zend_vm.h"
30
31
/* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
32
static zend_always_inline bool in_hitlist(const zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
33
2.79k
{
34
2.79k
  int i;
35
36
3.75k
  for (i = 0; i < jmp_hitlist_count; i++) {
37
1.72k
    if (jmp_hitlist[i] == target) {
38
765
      return true;
39
765
    }
40
1.72k
  }
41
2.02k
  return false;
42
2.79k
}
43
44
#define CHECK_LOOP(target) \
45
2.79k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
46
2.02k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
47
2.02k
  } else { \
48
765
    break; \
49
765
  }
50
51
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
52
25.9k
{
53
25.9k
  zend_op *opline;
54
25.9k
  zend_op *target;
55
25.9k
  zend_op **jmp_hitlist;
56
25.9k
  int jmp_hitlist_count;
57
25.9k
  ALLOCA_FLAG(use_heap);
58
59
25.9k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
60
25.9k
  opline = op_array->opcodes;
61
25.9k
  const zend_op *end =  opline + op_array->last;
62
63
191k
  while (opline < end) {
64
65
165k
    switch (opline->opcode) {
66
2.95k
      case ZEND_JMP:
67
2.95k
        jmp_hitlist_count = 0;
68
69
2.95k
        target = ZEND_OP1_JMP_ADDR(opline);
70
4.13k
        while (1) {
71
4.13k
          if (target->opcode == ZEND_JMP) {
72
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
73
1.90k
            target = ZEND_OP1_JMP_ADDR(target);
74
1.90k
            CHECK_LOOP(target);
75
2.23k
          } else if (target->opcode == ZEND_NOP) {
76
17
            target = target + 1;
77
2.21k
          } else {
78
2.21k
            break;
79
2.21k
          }
80
1.18k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
81
1.18k
        }
82
83
2.95k
        if (target == opline + 1) {
84
          /* convert L: JMP L+1 to NOP */
85
1.33k
          MAKE_NOP(opline);
86
1.61k
        } else if ((target->opcode == ZEND_RETURN ||
87
1.59k
                    target->opcode == ZEND_RETURN_BY_REF ||
88
1.59k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
89
24
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
90
          /* JMP L, L: RETURN to immediate RETURN */
91
24
          *opline = *target;
92
24
          if (opline->op1_type == IS_CONST) {
93
24
            zval zv;
94
24
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
95
24
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
96
24
          }
97
1.59k
        } else if (opline > op_array->opcodes &&
98
1.39k
                   ((opline-1)->opcode == ZEND_JMPZ ||
99
1.38k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
100
20
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
101
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
102
0
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
103
0
            }
104
20
        }
105
2.95k
        break;
106
107
249
      case ZEND_JMP_SET:
108
957
      case ZEND_COALESCE:
109
957
        jmp_hitlist_count = 0;
110
111
957
        target = ZEND_OP2_JMP_ADDR(opline);
112
957
        while (1) {
113
957
          if (target->opcode == ZEND_JMP) {
114
0
            target = ZEND_OP1_JMP_ADDR(target);
115
0
            CHECK_LOOP(target);
116
957
          } else if (target->opcode == ZEND_NOP) {
117
0
            target = target + 1;
118
957
          } else {
119
957
            break;
120
957
          }
121
0
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
122
0
        }
123
957
        break;
124
125
243
      case ZEND_JMPZ:
126
908
      case ZEND_JMPNZ:
127
908
        jmp_hitlist_count = 0;
128
129
908
        target = ZEND_OP2_JMP_ADDR(opline);
130
933
        while (1) {
131
933
          if (target->opcode == ZEND_JMP) {
132
            /* plain JMP */
133
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
134
4
            target = ZEND_OP1_JMP_ADDR(target);
135
4
            CHECK_LOOP(target);
136
929
          } else if (target->opcode == opline->opcode &&
137
24
                     SAME_VAR(opline->op1, target->op1)) {
138
            /* same opcode and same var as this opcode */
139
            /* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
140
24
            target = ZEND_OP2_JMP_ADDR(target);
141
24
            CHECK_LOOP(target);
142
905
          } else if (target->opcode == INV_COND(opline->opcode) &&
143
1
                     SAME_VAR(opline->op1, target->op1)) {
144
            /* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
145
               JMPZ(X,L1+1) */
146
0
            target = target + 1;
147
905
          } else if (target->opcode == ZEND_NOP) {
148
9
            target = target + 1;
149
896
          } else {
150
896
            break;
151
896
          }
152
25
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
153
25
        }
154
155
        /* convert L: JMPZ L+1 to NOP */
156
908
        if (target == opline + 1) {
157
2
          zend_optimizer_convert_to_free_op1(op_array, opline);
158
2
        }
159
908
        break;
160
161
675
      case ZEND_JMPZ_EX:
162
797
      case ZEND_JMPNZ_EX:
163
797
        jmp_hitlist_count = 0;
164
165
797
        target = ZEND_OP2_JMP_ADDR(opline);
166
1.66k
        while (1) {
167
1.66k
          if (target->opcode == ZEND_JMP) {
168
            /* plain JMP */
169
            /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
170
49
            target = ZEND_OP1_JMP_ADDR(target);
171
49
            CHECK_LOOP(target);
172
1.61k
          } else if (target->opcode == opline->opcode-3 &&
173
2
                     (SAME_VAR(target->op1, opline->result) ||
174
2
                      SAME_VAR(target->op1, opline->op1))) {
175
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
176
               JMPZ_EX(X,L2) */
177
2
            target = ZEND_OP2_JMP_ADDR(target);
178
2
            CHECK_LOOP(target);
179
1.61k
          } else if (target->opcode == opline->opcode &&
180
810
                     target->result.var == opline->result.var &&
181
810
                     (SAME_VAR(target->op1, opline->result) ||
182
806
                      SAME_VAR(target->op1, opline->op1))) {
183
            /* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
184
               JMPZ_EX(X,L2) */
185
806
            target = ZEND_OP2_JMP_ADDR(target);
186
806
            CHECK_LOOP(target);
187
807
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
188
4
                     (SAME_VAR(target->op1, opline->result) ||
189
4
                      SAME_VAR(target->op1, opline->op1))) {
190
             /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
191
              JMPZ_EX(X,L1+1) */
192
4
            target = target + 1;
193
803
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
194
20
                     target->result.var == opline->result.var &&
195
20
                     (SAME_VAR(target->op1, opline->result) ||
196
20
                      SAME_VAR(target->op1, opline->op1))) {
197
             /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
198
              JMPZ_EX(X,L1+1) */
199
20
            target = target + 1;
200
783
          } else if (target->opcode == ZEND_BOOL &&
201
18
                     (SAME_VAR(target->op1, opline->result) ||
202
16
                      SAME_VAR(target->op1, opline->op1))) {
203
            /* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
204
               Z = JMPZ_EX(X,L1+1) */
205
206
            /* NOTE: This optimization pattern is not safe, but works, */
207
            /*       because result of JMPZ_EX instruction             */
208
            /*       is not used on the following path and             */
209
            /*       should be used once on the branch path.           */
210
            /*                                                         */
211
            /*       The pattern works well only if jumps processed in */
212
            /*       direct order, otherwise it breaks JMPZ_EX         */
213
            /*       sequences too early.                              */
214
2
            opline->result.var = target->result.var;
215
2
            target = target + 1;
216
2
            CHECK_LOOP(target);
217
781
          } else if (target->opcode == ZEND_NOP) {
218
0
            target = target + 1;
219
781
          } else {
220
781
            break;
221
781
          }
222
867
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
223
867
        }
224
225
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
226
797
        if (target == opline + 1) {
227
0
          opline->opcode = ZEND_BOOL;
228
0
          opline->op2.num = 0;
229
0
        }
230
797
        break;
231
165k
    }
232
165k
    opline++;
233
165k
  }
234
25.9k
  free_alloca(jmp_hitlist, use_heap);
235
25.9k
}