Coverage Report

Created: 2026-06-02 06:40

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
15.8k
{
34
15.8k
  int i;
35
36
41.4k
  for (i = 0; i < jmp_hitlist_count; i++) {
37
25.5k
    if (jmp_hitlist[i] == target) {
38
27
      return true;
39
27
    }
40
25.5k
  }
41
15.8k
  return false;
42
15.8k
}
43
44
#define CHECK_LOOP(target) \
45
15.8k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
46
15.8k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
47
15.8k
  } else { \
48
27
    break; \
49
27
  }
50
51
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
52
45.3k
{
53
45.3k
  zend_op *opline;
54
45.3k
  zend_op *end;
55
45.3k
  zend_op *target;
56
45.3k
  zend_op **jmp_hitlist;
57
45.3k
  int jmp_hitlist_count;
58
45.3k
  ALLOCA_FLAG(use_heap);
59
60
45.3k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
61
45.3k
  opline = op_array->opcodes;
62
45.3k
  end =  opline + op_array->last;
63
64
1.22M
  while (opline < end) {
65
66
1.18M
    switch (opline->opcode) {
67
31.4k
      case ZEND_JMP:
68
31.4k
        jmp_hitlist_count = 0;
69
70
31.4k
        target = ZEND_OP1_JMP_ADDR(opline);
71
36.8k
        while (1) {
72
36.8k
          if (target->opcode == ZEND_JMP) {
73
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
74
5.25k
            target = ZEND_OP1_JMP_ADDR(target);
75
5.25k
            CHECK_LOOP(target);
76
31.5k
          } else if (target->opcode == ZEND_NOP) {
77
111
            target = target + 1;
78
31.4k
          } else {
79
31.4k
            break;
80
31.4k
          }
81
5.36k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
82
5.36k
        }
83
84
31.4k
        if (target == opline + 1) {
85
          /* convert L: JMP L+1 to NOP */
86
938
          MAKE_NOP(opline);
87
30.5k
        } else if ((target->opcode == ZEND_RETURN ||
88
28.0k
                    target->opcode == ZEND_RETURN_BY_REF ||
89
27.9k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
90
2.64k
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
91
          /* JMP L, L: RETURN to immediate RETURN */
92
2.46k
          *opline = *target;
93
2.46k
          if (opline->op1_type == IS_CONST) {
94
2.32k
            zval zv;
95
2.32k
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
96
2.32k
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
97
2.32k
          }
98
28.0k
        } else if (opline > op_array->opcodes &&
99
28.0k
                   ((opline-1)->opcode == ZEND_JMPZ ||
100
27.8k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
101
438
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
102
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
103
29
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
104
29
            }
105
438
        }
106
31.4k
        break;
107
108
892
      case ZEND_JMP_SET:
109
3.10k
      case ZEND_COALESCE:
110
3.10k
        jmp_hitlist_count = 0;
111
112
3.10k
        target = ZEND_OP2_JMP_ADDR(opline);
113
3.10k
        while (1) {
114
3.10k
          if (target->opcode == ZEND_JMP) {
115
0
            target = ZEND_OP1_JMP_ADDR(target);
116
0
            CHECK_LOOP(target);
117
3.10k
          } else if (target->opcode == ZEND_NOP) {
118
0
            target = target + 1;
119
3.10k
          } else {
120
3.10k
            break;
121
3.10k
          }
122
0
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
123
0
        }
124
3.10k
        break;
125
126
9.13k
      case ZEND_JMPZ:
127
14.1k
      case ZEND_JMPNZ:
128
14.1k
        jmp_hitlist_count = 0;
129
130
14.1k
        target = ZEND_OP2_JMP_ADDR(opline);
131
19.5k
        while (1) {
132
19.5k
          if (target->opcode == ZEND_JMP) {
133
            /* plain JMP */
134
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
135
5.25k
            target = ZEND_OP1_JMP_ADDR(target);
136
5.25k
            CHECK_LOOP(target);
137
14.2k
          } else if (target->opcode == opline->opcode &&
138
149
                     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
56
            target = ZEND_OP2_JMP_ADDR(target);
142
56
            CHECK_LOOP(target);
143
14.2k
          } else if (target->opcode == INV_COND(opline->opcode) &&
144
26
                     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
14.2k
          } else if (target->opcode == ZEND_NOP) {
149
43
            target = target + 1;
150
14.1k
          } else {
151
14.1k
            break;
152
14.1k
          }
153
5.33k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
154
5.33k
        }
155
156
        /* convert L: JMPZ L+1 to NOP */
157
14.1k
        if (target == opline + 1) {
158
52
          zend_optimizer_convert_to_free_op1(op_array, opline);
159
52
        }
160
14.1k
        break;
161
162
1.31k
      case ZEND_JMPZ_EX:
163
2.95k
      case ZEND_JMPNZ_EX:
164
2.95k
        jmp_hitlist_count = 0;
165
166
2.95k
        target = ZEND_OP2_JMP_ADDR(opline);
167
8.72k
        while (1) {
168
8.72k
          if (target->opcode == ZEND_JMP) {
169
            /* plain JMP */
170
            /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
171
110
            target = ZEND_OP1_JMP_ADDR(target);
172
110
            CHECK_LOOP(target);
173
8.61k
          } else if (target->opcode == opline->opcode-3 &&
174
434
                     (SAME_VAR(target->op1, opline->result) ||
175
428
                      SAME_VAR(target->op1, opline->op1))) {
176
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
177
               JMPZ_EX(X,L2) */
178
428
            target = ZEND_OP2_JMP_ADDR(target);
179
428
            CHECK_LOOP(target);
180
8.18k
          } else if (target->opcode == opline->opcode &&
181
4.58k
                     target->result.var == opline->result.var &&
182
4.57k
                     (SAME_VAR(target->op1, opline->result) ||
183
4.57k
                      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
4.57k
            target = ZEND_OP2_JMP_ADDR(target);
187
4.57k
            CHECK_LOOP(target);
188
4.57k
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
189
189
                     (SAME_VAR(target->op1, opline->result) ||
190
189
                      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
189
            target = target + 1;
194
3.42k
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
195
250
                     target->result.var == opline->result.var &&
196
250
                     (SAME_VAR(target->op1, opline->result) ||
197
250
                      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
250
            target = target + 1;
201
3.17k
          } else if (target->opcode == ZEND_BOOL &&
202
372
                     (SAME_VAR(target->op1, opline->result) ||
203
220
                      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
220
            opline->result.var = target->result.var;
216
220
            target = target + 1;
217
220
            CHECK_LOOP(target);
218
2.95k
          } else if (target->opcode == ZEND_NOP) {
219
2
            target = target + 1;
220
2.95k
          } else {
221
2.95k
            break;
222
2.95k
          }
223
5.76k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
224
5.76k
        }
225
226
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
227
2.95k
        if (target == opline + 1) {
228
0
          opline->opcode = ZEND_BOOL;
229
0
          opline->op2.num = 0;
230
0
        }
231
2.95k
        break;
232
1.18M
    }
233
1.18M
    opline++;
234
1.18M
  }
235
45.3k
  free_alloca(jmp_hitlist, use_heap);
236
45.3k
}