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/optimize_temp_vars_5.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
#include "Optimizer/zend_optimizer.h"
21
#include "Optimizer/zend_optimizer_internal.h"
22
#include "zend_API.h"
23
#include "zend_constants.h"
24
#include "zend_execute.h"
25
#include "zend_vm.h"
26
#include "zend_bitset.h"
27
#include "zend_observer.h"
28
29
2.55M
#define INVALID_VAR ((uint32_t)-1)
30
#define GET_AVAILABLE_T()         \
31
91.4M
  for (i = 0; i < T; i++) {       \
32
91.4M
    if (!zend_bitset_in(taken_T, i)) { \
33
836k
      break;              \
34
836k
    }                  \
35
91.4M
  }                    \
36
836k
  zend_bitset_incl(taken_T, i);     \
37
836k
  if (i > max) {             \
38
218k
    max = i;              \
39
218k
  }
40
41
void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
42
116k
{
43
116k
  uint32_t T = op_array->T;
44
116k
  int offset = op_array->last_var;
45
116k
  uint32_t bitset_len;
46
116k
  zend_bitset taken_T;  /* T index in use */
47
116k
  zend_op **start_of_T; /* opline where T is first used */
48
116k
  int *map_T;       /* Map's the T to its new index */
49
116k
  zend_op *opline, *end;
50
116k
  int currT;
51
116k
  int i;
52
116k
  int max = -1;
53
116k
  void *checkpoint = zend_arena_checkpoint(ctx->arena);
54
55
116k
  bitset_len = zend_bitset_len(T);
56
116k
  taken_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
57
116k
  start_of_T = (zend_op **) zend_arena_alloc(&ctx->arena, T * sizeof(zend_op *));
58
116k
  map_T = (int *) zend_arena_alloc(&ctx->arena, T * sizeof(int));
59
116k
  memset(map_T, 0xff, T * sizeof(int));
60
61
116k
  end = op_array->opcodes;
62
116k
  opline = &op_array->opcodes[op_array->last - 1];
63
64
  /* Find T definition points */
65
2.92M
  while (opline >= end) {
66
2.80M
    if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
67
1.32M
      start_of_T[VAR_NUM(opline->result.var) - offset] = opline;
68
1.32M
    }
69
2.80M
    opline--;
70
2.80M
  }
71
72
116k
  zend_bitset_clear(taken_T, bitset_len);
73
74
116k
  end = op_array->opcodes;
75
116k
  opline = &op_array->opcodes[op_array->last - 1];
76
77
2.92M
  while (opline >= end) {
78
2.80M
    if ((opline->op1_type & (IS_VAR | IS_TMP_VAR))) {
79
898k
      currT = VAR_NUM(opline->op1.var) - offset;
80
898k
      if (opline->opcode == ZEND_ROPE_END) {
81
34.5k
        int num = (((opline->extended_value + 1) * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
82
34.5k
        int var;
83
84
34.5k
        var = max;
85
195k
        while (var >= 0 && !zend_bitset_in(taken_T, var)) {
86
160k
          var--;
87
160k
        }
88
34.5k
        max = MAX(max, var + num);
89
34.5k
        var = var + 1;
90
34.5k
        map_T[currT] = var;
91
34.5k
        zend_bitset_incl(taken_T, var);
92
34.5k
        opline->op1.var = NUM_VAR(var + offset);
93
233k
        while (num > 1) {
94
199k
          num--;
95
199k
          zend_bitset_incl(taken_T, var + num);
96
199k
        }
97
864k
      } else {
98
864k
        if (map_T[currT] == INVALID_VAR) {
99
461k
          int use_new_var = 0;
100
101
          /* Code in "finally" blocks may modify temporary variables.
102
           * We allocate new temporaries for values that need to
103
           * relive FAST_CALLs.
104
           */
105
461k
          if ((op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) &&
106
2.54k
              (opline->opcode == ZEND_RETURN ||
107
2.46k
               opline->opcode == ZEND_GENERATOR_RETURN ||
108
2.43k
               opline->opcode == ZEND_RETURN_BY_REF ||
109
2.40k
               opline->opcode == ZEND_FREE ||
110
2.31k
               opline->opcode == ZEND_FE_FREE)) {
111
367
            zend_op *curr = opline;
112
113
440
            while (--curr >= end) {
114
440
              if (curr->opcode == ZEND_FAST_CALL) {
115
123
                use_new_var = 1;
116
123
                break;
117
317
              } else if (curr->opcode != ZEND_FREE &&
118
315
                         curr->opcode != ZEND_FE_FREE &&
119
293
                         curr->opcode != ZEND_VERIFY_RETURN_TYPE &&
120
266
                         curr->opcode != ZEND_DISCARD_EXCEPTION) {
121
244
                break;
122
244
              }
123
440
            }
124
367
          }
125
461k
          if (use_new_var) {
126
123
            i = ++max;
127
123
            zend_bitset_incl(taken_T, i);
128
461k
          } else {
129
461k
            GET_AVAILABLE_T();
130
461k
          }
131
461k
          map_T[currT] = i;
132
461k
        }
133
864k
        opline->op1.var = NUM_VAR(map_T[currT] + offset);
134
864k
      }
135
898k
    }
136
137
2.80M
    if ((opline->op2_type & (IS_VAR | IS_TMP_VAR))) {
138
360k
      currT = VAR_NUM(opline->op2.var) - offset;
139
360k
      if (map_T[currT] == INVALID_VAR) {
140
358k
        GET_AVAILABLE_T();
141
358k
        map_T[currT] = i;
142
358k
      }
143
360k
      opline->op2.var = NUM_VAR(map_T[currT] + offset);
144
360k
    }
145
146
2.80M
    if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
147
1.32M
      currT = VAR_NUM(opline->result.var) - offset;
148
1.32M
      if (map_T[currT] == INVALID_VAR) {
149
        /* As a result of DCE, an opcode may have an unused result. */
150
15.8k
        GET_AVAILABLE_T();
151
15.8k
        map_T[currT] = i;
152
15.8k
      }
153
1.32M
      opline->result.var = NUM_VAR(map_T[currT] + offset);
154
1.32M
      if (start_of_T[currT] == opline) {
155
        /* ZEND_FAST_CALL can not share temporary var with others
156
         * since the fast_var could also be set by ZEND_HANDLE_EXCEPTION
157
         * which could be ahead of it */
158
870k
        if (opline->opcode != ZEND_FAST_CALL) {
159
869k
          zend_bitset_excl(taken_T, map_T[currT]);
160
869k
        }
161
870k
        if (opline->opcode == ZEND_ROPE_INIT) {
162
34.5k
          uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
163
233k
          while (num > 1) {
164
199k
            num--;
165
199k
            zend_bitset_excl(taken_T, map_T[currT]+num);
166
199k
          }
167
34.5k
        }
168
870k
      }
169
1.32M
    }
170
171
2.80M
    opline--;
172
2.80M
  }
173
174
116k
  zend_arena_release(&ctx->arena, checkpoint);
175
116k
  op_array->T = max + 1 + ZEND_OBSERVER_ENABLED; // reserve last temporary for observers if enabled
176
116k
}