Coverage Report

Created: 2026-06-02 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
19.0k
{
34
19.0k
  int i;
35
36
68.0k
  for (i = 0; i < jmp_hitlist_count; i++) {
37
49.1k
    if (jmp_hitlist[i] == target) {
38
72
      return true;
39
72
    }
40
49.1k
  }
41
18.9k
  return false;
42
19.0k
}
43
44
#define CHECK_LOOP(target) \
45
19.0k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
46
18.9k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
47
18.9k
  } else { \
48
72
    break; \
49
72
  }
50
51
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
52
47.7k
{
53
47.7k
  zend_op *opline;
54
47.7k
  zend_op *end;
55
47.7k
  zend_op *target;
56
47.7k
  zend_op **jmp_hitlist;
57
47.7k
  int jmp_hitlist_count;
58
47.7k
  ALLOCA_FLAG(use_heap);
59
60
47.7k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
61
47.7k
  opline = op_array->opcodes;
62
47.7k
  end =  opline + op_array->last;
63
64
1.18M
  while (opline < end) {
65
66
1.13M
    switch (opline->opcode) {
67
30.2k
      case ZEND_JMP:
68
30.2k
        jmp_hitlist_count = 0;
69
70
30.2k
        target = ZEND_OP1_JMP_ADDR(opline);
71
35.9k
        while (1) {
72
35.9k
          if (target->opcode == ZEND_JMP) {
73
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
74
5.50k
            target = ZEND_OP1_JMP_ADDR(target);
75
5.50k
            CHECK_LOOP(target);
76
30.4k
          } else if (target->opcode == ZEND_NOP) {
77
152
            target = target + 1;
78
30.2k
          } else {
79
30.2k
            break;
80
30.2k
          }
81
5.62k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
82
5.62k
        }
83
84
30.2k
        if (target == opline + 1) {
85
          /* convert L: JMP L+1 to NOP */
86
1.58k
          MAKE_NOP(opline);
87
28.7k
        } else if ((target->opcode == ZEND_RETURN ||
88
26.1k
                    target->opcode == ZEND_RETURN_BY_REF ||
89
26.0k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
90
2.75k
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
91
          /* JMP L, L: RETURN to immediate RETURN */
92
2.53k
          *opline = *target;
93
2.53k
          if (opline->op1_type == IS_CONST) {
94
2.40k
            zval zv;
95
2.40k
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
96
2.40k
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
97
2.40k
          }
98
26.1k
        } else if (opline > op_array->opcodes &&
99
26.0k
                   ((opline-1)->opcode == ZEND_JMPZ ||
100
25.8k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
101
397
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
102
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
103
36
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
104
36
            }
105
397
        }
106
30.2k
        break;
107
108
958
      case ZEND_JMP_SET:
109
2.83k
      case ZEND_COALESCE:
110
2.83k
        jmp_hitlist_count = 0;
111
112
2.83k
        target = ZEND_OP2_JMP_ADDR(opline);
113
2.83k
        while (1) {
114
2.83k
          if (target->opcode == ZEND_JMP) {
115
0
            target = ZEND_OP1_JMP_ADDR(target);
116
0
            CHECK_LOOP(target);
117
2.83k
          } else if (target->opcode == ZEND_NOP) {
118
0
            target = target + 1;
119
2.83k
          } else {
120
2.83k
            break;
121
2.83k
          }
122
0
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
123
0
        }
124
2.83k
        break;
125
126
8.56k
      case ZEND_JMPZ:
127
12.5k
      case ZEND_JMPNZ:
128
12.5k
        jmp_hitlist_count = 0;
129
130
12.5k
        target = ZEND_OP2_JMP_ADDR(opline);
131
17.4k
        while (1) {
132
17.4k
          if (target->opcode == ZEND_JMP) {
133
            /* plain JMP */
134
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
135
4.84k
            target = ZEND_OP1_JMP_ADDR(target);
136
4.84k
            CHECK_LOOP(target);
137
12.6k
          } else if (target->opcode == opline->opcode &&
138
193
                     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
82
            target = ZEND_OP2_JMP_ADDR(target);
142
82
            CHECK_LOOP(target);
143
12.5k
          } else if (target->opcode == INV_COND(opline->opcode) &&
144
76
                     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
12.5k
          } else if (target->opcode == ZEND_NOP) {
149
62
            target = target + 1;
150
12.5k
          } else {
151
12.5k
            break;
152
12.5k
          }
153
4.94k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
154
4.94k
        }
155
156
        /* convert L: JMPZ L+1 to NOP */
157
12.5k
        if (target == opline + 1) {
158
37
          zend_optimizer_convert_to_free_op1(op_array, opline);
159
37
        }
160
12.5k
        break;
161
162
1.61k
      case ZEND_JMPZ_EX:
163
3.14k
      case ZEND_JMPNZ_EX:
164
3.14k
        jmp_hitlist_count = 0;
165
166
3.14k
        target = ZEND_OP2_JMP_ADDR(opline);
167
12.4k
        while (1) {
168
12.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
96
            target = ZEND_OP1_JMP_ADDR(target);
172
96
            CHECK_LOOP(target);
173
12.3k
          } else if (target->opcode == opline->opcode-3 &&
174
347
                     (SAME_VAR(target->op1, opline->result) ||
175
343
                      SAME_VAR(target->op1, opline->op1))) {
176
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
177
               JMPZ_EX(X,L2) */
178
343
            target = ZEND_OP2_JMP_ADDR(target);
179
343
            CHECK_LOOP(target);
180
12.0k
          } else if (target->opcode == opline->opcode &&
181
7.72k
                     target->result.var == opline->result.var &&
182
7.72k
                     (SAME_VAR(target->op1, opline->result) ||
183
7.72k
                      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
7.72k
            target = ZEND_OP2_JMP_ADDR(target);
187
7.72k
            CHECK_LOOP(target);
188
7.72k
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
189
162
                     (SAME_VAR(target->op1, opline->result) ||
190
162
                      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
162
            target = target + 1;
194
4.16k
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
195
534
                     target->result.var == opline->result.var &&
196
534
                     (SAME_VAR(target->op1, opline->result) ||
197
534
                      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
534
            target = target + 1;
201
3.63k
          } else if (target->opcode == ZEND_BOOL &&
202
726
                     (SAME_VAR(target->op1, opline->result) ||
203
478
                      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
478
            opline->result.var = target->result.var;
216
478
            target = target + 1;
217
478
            CHECK_LOOP(target);
218
3.15k
          } else if (target->opcode == ZEND_NOP) {
219
4
            target = target + 1;
220
3.14k
          } else {
221
3.14k
            break;
222
3.14k
          }
223
9.34k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
224
9.34k
        }
225
226
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
227
3.14k
        if (target == opline + 1) {
228
0
          opline->opcode = ZEND_BOOL;
229
0
          opline->op2.num = 0;
230
0
        }
231
3.14k
        break;
232
1.13M
    }
233
1.13M
    opline++;
234
1.13M
  }
235
47.7k
  free_alloca(jmp_hitlist, use_heap);
236
47.7k
}