Coverage Report

Created: 2026-02-09 07:07

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 (c) The PHP Group                                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 3.01 of the PHP license,      |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | https://www.php.net/license/3_01.txt                                 |
11
   | If you did not receive a copy of the PHP license and are unable to   |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@php.net so we can mail you a copy immediately.               |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Stanislav Malyshev <stas@zend.com>                          |
18
   |          Dmitry Stogov <dmitry@php.net>                              |
19
   +----------------------------------------------------------------------+
20
*/
21
22
/* pass 3: (Jump optimization)
23
 * - optimize series of JMPs
24
 */
25
26
#include "Optimizer/zend_optimizer.h"
27
#include "Optimizer/zend_optimizer_internal.h"
28
#include "zend_API.h"
29
#include "zend_constants.h"
30
#include "zend_execute.h"
31
#include "zend_vm.h"
32
33
/* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
34
static zend_always_inline bool in_hitlist(zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
35
37.8k
{
36
37.8k
  int i;
37
38
123k
  for (i = 0; i < jmp_hitlist_count; i++) {
39
86.1k
    if (jmp_hitlist[i] == target) {
40
89
      return true;
41
89
    }
42
86.1k
  }
43
37.7k
  return false;
44
37.8k
}
45
46
#define CHECK_LOOP(target) \
47
37.8k
  if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
48
37.7k
    jmp_hitlist[jmp_hitlist_count++] = target;  \
49
37.7k
  } else { \
50
89
    break; \
51
89
  }
52
53
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
54
89.7k
{
55
89.7k
  zend_op *opline;
56
89.7k
  zend_op *end;
57
89.7k
  zend_op *target;
58
89.7k
  zend_op **jmp_hitlist;
59
89.7k
  int jmp_hitlist_count;
60
89.7k
  ALLOCA_FLAG(use_heap);
61
62
89.7k
  jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
63
89.7k
  opline = op_array->opcodes;
64
89.7k
  end =  opline + op_array->last;
65
66
2.41M
  while (opline < end) {
67
68
2.32M
    switch (opline->opcode) {
69
63.3k
      case ZEND_JMP:
70
63.3k
        jmp_hitlist_count = 0;
71
72
63.3k
        target = ZEND_OP1_JMP_ADDR(opline);
73
75.6k
        while (1) {
74
75.6k
          if (target->opcode == ZEND_JMP) {
75
            /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
76
12.0k
            target = ZEND_OP1_JMP_ADDR(target);
77
12.0k
            CHECK_LOOP(target);
78
63.5k
          } else if (target->opcode == ZEND_NOP) {
79
286
            target = target + 1;
80
63.3k
          } else {
81
63.3k
            break;
82
63.3k
          }
83
12.3k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
84
12.3k
        }
85
86
63.3k
        if (target == opline + 1) {
87
          /* convert L: JMP L+1 to NOP */
88
1.90k
          MAKE_NOP(opline);
89
61.4k
        } else if ((target->opcode == ZEND_RETURN ||
90
56.3k
                    target->opcode == ZEND_RETURN_BY_REF ||
91
56.2k
                    target->opcode == ZEND_GENERATOR_RETURN) &&
92
5.32k
                   !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
93
          /* JMP L, L: RETURN to immediate RETURN */
94
4.93k
          *opline = *target;
95
4.93k
          if (opline->op1_type == IS_CONST) {
96
4.68k
            zval zv;
97
4.68k
            ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
98
4.68k
            opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
99
4.68k
          }
100
56.4k
        } else if (opline > op_array->opcodes &&
101
56.3k
                   ((opline-1)->opcode == ZEND_JMPZ ||
102
56.0k
                    (opline-1)->opcode == ZEND_JMPNZ)) {
103
693
            if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
104
            /* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
105
65
            zend_optimizer_convert_to_free_op1(op_array, opline - 1);
106
65
            }
107
693
        }
108
63.3k
        break;
109
110
1.60k
      case ZEND_JMP_SET:
111
5.07k
      case ZEND_COALESCE:
112
5.07k
        jmp_hitlist_count = 0;
113
114
5.07k
        target = ZEND_OP2_JMP_ADDR(opline);
115
5.07k
        while (1) {
116
5.07k
          if (target->opcode == ZEND_JMP) {
117
0
            target = ZEND_OP1_JMP_ADDR(target);
118
0
            CHECK_LOOP(target);
119
5.07k
          } else if (target->opcode == ZEND_NOP) {
120
0
            target = target + 1;
121
5.07k
          } else {
122
5.07k
            break;
123
5.07k
          }
124
0
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
125
0
        }
126
5.07k
        break;
127
128
17.8k
      case ZEND_JMPZ:
129
25.9k
      case ZEND_JMPNZ:
130
25.9k
        jmp_hitlist_count = 0;
131
132
25.9k
        target = ZEND_OP2_JMP_ADDR(opline);
133
37.7k
        while (1) {
134
37.7k
          if (target->opcode == ZEND_JMP) {
135
            /* plain JMP */
136
            /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
137
11.5k
            target = ZEND_OP1_JMP_ADDR(target);
138
11.5k
            CHECK_LOOP(target);
139
26.1k
          } else if (target->opcode == opline->opcode &&
140
358
                     SAME_VAR(opline->op1, target->op1)) {
141
            /* same opcode and same var as this opcode */
142
            /* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
143
150
            target = ZEND_OP2_JMP_ADDR(target);
144
150
            CHECK_LOOP(target);
145
26.0k
          } else if (target->opcode == INV_COND(opline->opcode) &&
146
63
                     SAME_VAR(opline->op1, target->op1)) {
147
            /* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
148
               JMPZ(X,L1+1) */
149
0
            target = target + 1;
150
26.0k
          } else if (target->opcode == ZEND_NOP) {
151
120
            target = target + 1;
152
25.8k
          } else {
153
25.8k
            break;
154
25.8k
          }
155
11.7k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
156
11.7k
        }
157
158
        /* convert L: JMPZ L+1 to NOP */
159
25.9k
        if (target == opline + 1) {
160
62
          zend_optimizer_convert_to_free_op1(op_array, opline);
161
62
        }
162
25.9k
        break;
163
164
2.79k
      case ZEND_JMPZ_EX:
165
5.61k
      case ZEND_JMPNZ_EX:
166
5.61k
        jmp_hitlist_count = 0;
167
168
5.61k
        target = ZEND_OP2_JMP_ADDR(opline);
169
20.7k
        while (1) {
170
20.7k
          if (target->opcode == ZEND_JMP) {
171
            /* plain JMP */
172
            /* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
173
220
            target = ZEND_OP1_JMP_ADDR(target);
174
220
            CHECK_LOOP(target);
175
20.5k
          } else if (target->opcode == opline->opcode-3 &&
176
594
                     (SAME_VAR(target->op1, opline->result) ||
177
586
                      SAME_VAR(target->op1, opline->op1))) {
178
            /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
179
               JMPZ_EX(X,L2) */
180
586
            target = ZEND_OP2_JMP_ADDR(target);
181
586
            CHECK_LOOP(target);
182
19.9k
          } else if (target->opcode == opline->opcode &&
183
12.6k
                     target->result.var == opline->result.var &&
184
12.6k
                     (SAME_VAR(target->op1, opline->result) ||
185
12.6k
                      SAME_VAR(target->op1, opline->op1))) {
186
            /* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
187
               JMPZ_EX(X,L2) */
188
12.6k
            target = ZEND_OP2_JMP_ADDR(target);
189
12.6k
            CHECK_LOOP(target);
190
12.6k
          } else if (target->opcode == INV_EX_COND(opline->opcode) &&
191
260
                     (SAME_VAR(target->op1, opline->result) ||
192
260
                      SAME_VAR(target->op1, opline->op1))) {
193
             /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
194
              JMPZ_EX(X,L1+1) */
195
260
            target = target + 1;
196
7.09k
          } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
197
836
                     target->result.var == opline->result.var &&
198
836
                     (SAME_VAR(target->op1, opline->result) ||
199
836
                      SAME_VAR(target->op1, opline->op1))) {
200
             /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
201
              JMPZ_EX(X,L1+1) */
202
836
            target = target + 1;
203
6.26k
          } else if (target->opcode == ZEND_BOOL &&
204
1.12k
                     (SAME_VAR(target->op1, opline->result) ||
205
647
                      SAME_VAR(target->op1, opline->op1))) {
206
            /* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
207
               Z = JMPZ_EX(X,L1+1) */
208
209
            /* NOTE: This optimization pattern is not safe, but works, */
210
            /*       because result of JMPZ_EX instruction             */
211
            /*       is not used on the following path and             */
212
            /*       should be used once on the branch path.           */
213
            /*                                                         */
214
            /*       The pattern works well only if jumps processed in */
215
            /*       direct order, otherwise it breaks JMPZ_EX         */
216
            /*       sequences too early.                              */
217
647
            opline->result.var = target->result.var;
218
647
            target = target + 1;
219
647
            CHECK_LOOP(target);
220
5.61k
          } else if (target->opcode == ZEND_NOP) {
221
6
            target = target + 1;
222
5.60k
          } else {
223
5.60k
            break;
224
5.60k
          }
225
15.1k
          ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
226
15.1k
        }
227
228
        /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
229
5.61k
        if (target == opline + 1) {
230
0
          opline->opcode = ZEND_BOOL;
231
0
          opline->op2.num = 0;
232
0
        }
233
5.61k
        break;
234
2.32M
    }
235
2.32M
    opline++;
236
2.32M
  }
237
89.7k
  free_alloca(jmp_hitlist, use_heap);
238
89.7k
}