Coverage Report

Created: 2026-07-25 06:39

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(zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
33
41.2k
{
34
41.2k
  int i;
35
36
139k
  for (i = 0; i < jmp_hitlist_count; i++) {
37
98.2k
    if (jmp_hitlist[i] == target) {
38
109
      return true;
39
109
    }
40
98.2k
  }
41
41.1k
  return false;
42
41.2k
}
43
44
#define CHECK_LOOP(target) \
45
41.2k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
46
41.1k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
47
41.1k
  } else { \
48
109
    break; \
49
109
  }
50
51
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
52
116k
{
53
116k
  zend_op *opline;
54
116k
  zend_op *end;
55
116k
  zend_op *target;
56
116k
  zend_op **jmp_hitlist;
57
116k
  int jmp_hitlist_count;
58
116k
  ALLOCA_FLAG(use_heap);
59
60
116k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
61
116k
  opline = op_array->opcodes;
62
116k
  end =  opline + op_array->last;
63
64
3.05M
  while (opline < end) {
65
66
2.94M
    switch (opline->opcode) {
67
76.9k
      case ZEND_JMP:
68
76.9k
        jmp_hitlist_count = 0;
69
70
76.9k
        target = ZEND_OP1_JMP_ADDR(opline);
71
89.5k
        while (1) {
72
89.5k
          if (target->opcode == ZEND_JMP) {
73
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
74
12.2k
            target = ZEND_OP1_JMP_ADDR(target);
75
12.2k
            CHECK_LOOP(target);
76
77.2k
          } else if (target->opcode == ZEND_NOP) {
77
317
            target = target + 1;
78
76.9k
          } else {
79
76.9k
            break;
80
76.9k
          }
81
12.5k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
82
12.5k
        }
83
84
76.9k
        if (target == opline + 1) {
85
          /* convert L: JMP L+1 to NOP */
86
3.08k
          MAKE_NOP(opline);
87
73.9k
        } else if ((target->opcode == ZEND_RETURN ||
88
66.5k
                    target->opcode == ZEND_RETURN_BY_REF ||
89
66.4k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
90
7.70k
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
91
          /* JMP L, L: RETURN to immediate RETURN */
92
7.23k
          *opline = *target;
93
7.23k
          if (opline->op1_type == IS_CONST) {
94
6.86k
            zval zv;
95
6.86k
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
96
6.86k
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
97
6.86k
          }
98
66.6k
        } else if (opline > op_array->opcodes &&
99
66.3k
                   ((opline-1)->opcode == ZEND_JMPZ ||
100
65.9k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
101
1.07k
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
102
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
103
70
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
104
70
            }
105
1.07k
        }
106
76.9k
        break;
107
108
3.10k
      case ZEND_JMP_SET:
109
8.63k
      case ZEND_COALESCE:
110
8.63k
        jmp_hitlist_count = 0;
111
112
8.63k
        target = ZEND_OP2_JMP_ADDR(opline);
113
8.63k
        while (1) {
114
8.63k
          if (target->opcode == ZEND_JMP) {
115
0
            target = ZEND_OP1_JMP_ADDR(target);
116
0
            CHECK_LOOP(target);
117
8.63k
          } else if (target->opcode == ZEND_NOP) {
118
0
            target = target + 1;
119
8.63k
          } else {
120
8.63k
            break;
121
8.63k
          }
122
0
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
123
0
        }
124
8.63k
        break;
125
126
22.8k
      case ZEND_JMPZ:
127
34.9k
      case ZEND_JMPNZ:
128
34.9k
        jmp_hitlist_count = 0;
129
130
34.9k
        target = ZEND_OP2_JMP_ADDR(opline);
131
46.6k
        while (1) {
132
46.6k
          if (target->opcode == ZEND_JMP) {
133
            /* plain JMP */
134
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
135
11.5k
            target = ZEND_OP1_JMP_ADDR(target);
136
11.5k
            CHECK_LOOP(target);
137
35.1k
          } else if (target->opcode == opline->opcode &&
138
379
                     SAME_VAR(opline->op1, target->op1)) {
139
            /* same opcode and same var as this opcode */
140
            /* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
141
164
            target = ZEND_OP2_JMP_ADDR(target);
142
164
            CHECK_LOOP(target);
143
34.9k
          } else if (target->opcode == INV_COND(opline->opcode) &&
144
171
                     SAME_VAR(opline->op1, target->op1)) {
145
            /* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
146
               JMPZ(X,L1+1) */
147
0
            target = target + 1;
148
34.9k
          } else if (target->opcode == ZEND_NOP) {
149
125
            target = target + 1;
150
34.8k
          } else {
151
34.8k
            break;
152
34.8k
          }
153
11.7k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
154
11.7k
        }
155
156
        /* convert L: JMPZ L+1 to NOP */
157
34.9k
        if (target == opline + 1) {
158
110
          zend_optimizer_convert_to_free_op1(op_array, opline);
159
110
        }
160
34.9k
        break;
161
162
3.84k
      case ZEND_JMPZ_EX:
163
7.59k
      case ZEND_JMPNZ_EX:
164
7.59k
        jmp_hitlist_count = 0;
165
166
7.59k
        target = ZEND_OP2_JMP_ADDR(opline);
167
26.4k
        while (1) {
168
26.4k
          if (target->opcode == ZEND_JMP) {
169
            /* plain JMP */
170
            /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
171
275
            target = ZEND_OP1_JMP_ADDR(target);
172
275
            CHECK_LOOP(target);
173
26.1k
          } else if (target->opcode == opline->opcode-3 &&
174
1.12k
                     (SAME_VAR(target->op1, opline->result) ||
175
1.10k
                      SAME_VAR(target->op1, opline->op1))) {
176
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
177
               JMPZ_EX(X,L2) */
178
1.10k
            target = ZEND_OP2_JMP_ADDR(target);
179
1.10k
            CHECK_LOOP(target);
180
25.0k
          } else if (target->opcode == opline->opcode &&
181
15.1k
                     target->result.var == opline->result.var &&
182
15.1k
                     (SAME_VAR(target->op1, opline->result) ||
183
15.1k
                      SAME_VAR(target->op1, opline->op1))) {
184
            /* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
185
               JMPZ_EX(X,L2) */
186
15.1k
            target = ZEND_OP2_JMP_ADDR(target);
187
15.1k
            CHECK_LOOP(target);
188
15.1k
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
189
551
                     (SAME_VAR(target->op1, opline->result) ||
190
541
                      SAME_VAR(target->op1, opline->op1))) {
191
             /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
192
              JMPZ_EX(X,L1+1) */
193
541
            target = target + 1;
194
9.33k
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
195
918
                     target->result.var == opline->result.var &&
196
918
                     (SAME_VAR(target->op1, opline->result) ||
197
918
                      SAME_VAR(target->op1, opline->op1))) {
198
             /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
199
              JMPZ_EX(X,L1+1) */
200
918
            target = target + 1;
201
8.41k
          } else if (target->opcode == ZEND_BOOL &&
202
1.28k
                     (SAME_VAR(target->op1, opline->result) ||
203
811
                      SAME_VAR(target->op1, opline->op1))) {
204
            /* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
205
               Z = JMPZ_EX(X,L1+1) */
206
207
            /* NOTE: This optimization pattern is not safe, but works, */
208
            /*       because result of JMPZ_EX instruction             */
209
            /*       is not used on the following path and             */
210
            /*       should be used once on the branch path.           */
211
            /*                                                         */
212
            /*       The pattern works well only if jumps processed in */
213
            /*       direct order, otherwise it breaks JMPZ_EX         */
214
            /*       sequences too early.                              */
215
811
            opline->result.var = target->result.var;
216
811
            target = target + 1;
217
811
            CHECK_LOOP(target);
218
7.60k
          } else if (target->opcode == ZEND_NOP) {
219
4
            target = target + 1;
220
7.59k
          } else {
221
7.59k
            break;
222
7.59k
          }
223
18.8k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
224
18.8k
        }
225
226
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
227
7.59k
        if (target == opline + 1) {
228
0
          opline->opcode = ZEND_BOOL;
229
0
          opline->op2.num = 0;
230
0
        }
231
7.59k
        break;
232
2.94M
    }
233
2.94M
    opline++;
234
2.94M
  }
235
116k
  free_alloca(jmp_hitlist, use_heap);
236
116k
}