Coverage Report

Created: 2026-09-14 06:25

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
40.0k
{
34
40.0k
  int i;
35
36
141k
  for (i = 0; i < jmp_hitlist_count; i++) {
37
101k
    if (jmp_hitlist[i] == target) {
38
116
      return true;
39
116
    }
40
101k
  }
41
39.9k
  return false;
42
40.0k
}
43
44
#define CHECK_LOOP(target) \
45
40.0k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
46
39.9k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
47
39.9k
  } else { \
48
116
    break; \
49
116
  }
50
51
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
52
115k
{
53
115k
  zend_op *opline;
54
115k
  zend_op *target;
55
115k
  zend_op **jmp_hitlist;
56
115k
  int jmp_hitlist_count;
57
115k
  ALLOCA_FLAG(use_heap);
58
59
115k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
60
115k
  opline = op_array->opcodes;
61
115k
  const zend_op *end =  opline + op_array->last;
62
63
2.94M
  while (opline < end) {
64
65
2.82M
    switch (opline->opcode) {
66
75.1k
      case ZEND_JMP:
67
75.1k
        jmp_hitlist_count = 0;
68
69
75.1k
        target = ZEND_OP1_JMP_ADDR(opline);
70
86.5k
        while (1) {
71
86.5k
          if (target->opcode == ZEND_JMP) {
72
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
73
11.1k
            target = ZEND_OP1_JMP_ADDR(target);
74
11.1k
            CHECK_LOOP(target);
75
75.3k
          } else if (target->opcode == ZEND_NOP) {
76
315
            target = target + 1;
77
75.0k
          } else {
78
75.0k
            break;
79
75.0k
          }
80
11.3k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
81
11.3k
        }
82
83
75.1k
        if (target == opline + 1) {
84
          /* convert L: JMP L+1 to NOP */
85
3.00k
          MAKE_NOP(opline);
86
72.1k
        } else if ((target->opcode == ZEND_RETURN ||
87
64.8k
                    target->opcode == ZEND_RETURN_BY_REF ||
88
64.6k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
89
7.60k
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
90
          /* JMP L, L: RETURN to immediate RETURN */
91
7.13k
          *opline = *target;
92
7.13k
          if (opline->op1_type == IS_CONST) {
93
6.78k
            zval zv;
94
6.78k
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
95
6.78k
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
96
6.78k
          }
97
64.9k
        } else if (opline > op_array->opcodes &&
98
64.6k
                   ((opline-1)->opcode == ZEND_JMPZ ||
99
64.2k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
100
1.16k
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
101
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
102
80
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
103
80
            }
104
1.16k
        }
105
75.1k
        break;
106
107
3.60k
      case ZEND_JMP_SET:
108
9.22k
      case ZEND_COALESCE:
109
9.22k
        jmp_hitlist_count = 0;
110
111
9.22k
        target = ZEND_OP2_JMP_ADDR(opline);
112
9.22k
        while (1) {
113
9.22k
          if (target->opcode == ZEND_JMP) {
114
2
            target = ZEND_OP1_JMP_ADDR(target);
115
2
            CHECK_LOOP(target);
116
9.22k
          } else if (target->opcode == ZEND_NOP) {
117
0
            target = target + 1;
118
9.22k
          } else {
119
9.22k
            break;
120
9.22k
          }
121
2
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
122
2
        }
123
9.22k
        break;
124
125
22.2k
      case ZEND_JMPZ:
126
34.8k
      case ZEND_JMPNZ:
127
34.8k
        jmp_hitlist_count = 0;
128
129
34.8k
        target = ZEND_OP2_JMP_ADDR(opline);
130
45.4k
        while (1) {
131
45.4k
          if (target->opcode == ZEND_JMP) {
132
            /* plain JMP */
133
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
134
10.3k
            target = ZEND_OP1_JMP_ADDR(target);
135
10.3k
            CHECK_LOOP(target);
136
35.0k
          } else if (target->opcode == opline->opcode &&
137
438
                     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
180
            target = ZEND_OP2_JMP_ADDR(target);
141
180
            CHECK_LOOP(target);
142
34.9k
          } else if (target->opcode == INV_COND(opline->opcode) &&
143
183
                     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
34.9k
          } else if (target->opcode == ZEND_NOP) {
148
125
            target = target + 1;
149
34.7k
          } else {
150
34.7k
            break;
151
34.7k
          }
152
10.6k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
153
10.6k
        }
154
155
        /* convert L: JMPZ L+1 to NOP */
156
34.8k
        if (target == opline + 1) {
157
157
          zend_optimizer_convert_to_free_op1(op_array, opline);
158
157
        }
159
34.8k
        break;
160
161
4.15k
      case ZEND_JMPZ_EX:
162
8.21k
      case ZEND_JMPNZ_EX:
163
8.21k
        jmp_hitlist_count = 0;
164
165
8.21k
        target = ZEND_OP2_JMP_ADDR(opline);
166
28.2k
        while (1) {
167
28.2k
          if (target->opcode == ZEND_JMP) {
168
            /* plain JMP */
169
            /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
170
262
            target = ZEND_OP1_JMP_ADDR(target);
171
262
            CHECK_LOOP(target);
172
27.9k
          } else if (target->opcode == opline->opcode-3 &&
173
1.25k
                     (SAME_VAR(target->op1, opline->result) ||
174
1.23k
                      SAME_VAR(target->op1, opline->op1))) {
175
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
176
               JMPZ_EX(X,L2) */
177
1.23k
            target = ZEND_OP2_JMP_ADDR(target);
178
1.23k
            CHECK_LOOP(target);
179
26.7k
          } else if (target->opcode == opline->opcode &&
180
16.1k
                     target->result.var == opline->result.var &&
181
16.0k
                     (SAME_VAR(target->op1, opline->result) ||
182
16.0k
                      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
16.0k
            target = ZEND_OP2_JMP_ADDR(target);
186
16.0k
            CHECK_LOOP(target);
187
16.0k
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
188
654
                     (SAME_VAR(target->op1, opline->result) ||
189
646
                      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
646
            target = target + 1;
193
10.0k
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
194
960
                     target->result.var == opline->result.var &&
195
960
                     (SAME_VAR(target->op1, opline->result) ||
196
960
                      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
960
            target = target + 1;
200
9.05k
          } else if (target->opcode == ZEND_BOOL &&
201
1.35k
                     (SAME_VAR(target->op1, opline->result) ||
202
833
                      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
833
            opline->result.var = target->result.var;
215
833
            target = target + 1;
216
833
            CHECK_LOOP(target);
217
8.22k
          } else if (target->opcode == ZEND_NOP) {
218
4
            target = target + 1;
219
8.21k
          } else {
220
8.21k
            break;
221
8.21k
          }
222
19.9k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
223
19.9k
        }
224
225
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
226
8.21k
        if (target == opline + 1) {
227
0
          opline->opcode = ZEND_BOOL;
228
0
          opline->op2.num = 0;
229
0
        }
230
8.21k
        break;
231
2.82M
    }
232
2.82M
    opline++;
233
2.82M
  }
234
115k
  free_alloca(jmp_hitlist, use_heap);
235
115k
}