Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/Optimizer/compact_vars.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, Removing unused variables                               |
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: Nikita Popov <nikic@php.net>                                |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "Optimizer/zend_optimizer_internal.h"
18
#include "zend_bitset.h"
19
#include "zend_observer.h"
20
21
/* This pass removes all CVs and temporaries that are completely unused. It does *not* merge any CVs or TMPs.
22
 * This pass does not operate on SSA form anymore. */
23
1
void zend_optimizer_compact_vars(zend_op_array *op_array) {
24
1
  int i;
25
26
1
  ALLOCA_FLAG(use_heap1);
27
1
  ALLOCA_FLAG(use_heap2);
28
1
  uint32_t used_vars_len = zend_bitset_len(op_array->last_var + op_array->T);
29
1
  zend_bitset used_vars = ZEND_BITSET_ALLOCA(used_vars_len, use_heap1);
30
1
  uint32_t *vars_map = do_alloca((op_array->last_var + op_array->T) * sizeof(uint32_t), use_heap2);
31
1
  uint32_t num_cvs, num_tmps;
32
33
  /* Determine which CVs are used */
34
1
  zend_bitset_clear(used_vars, used_vars_len);
35
2
  for (i = 0; i < op_array->last; i++) {
36
1
    zend_op *opline = &op_array->opcodes[i];
37
1
    if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
38
0
      zend_bitset_incl(used_vars, VAR_NUM(opline->op1.var));
39
0
    }
40
1
    if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
41
0
      zend_bitset_incl(used_vars, VAR_NUM(opline->op2.var));
42
0
    }
43
1
    if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
44
0
      zend_bitset_incl(used_vars, VAR_NUM(opline->result.var));
45
0
      if (opline->opcode == ZEND_ROPE_INIT) {
46
0
        uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
47
0
        while (num > 1) {
48
0
          num--;
49
0
          zend_bitset_incl(used_vars, VAR_NUM(opline->result.var) + num);
50
0
        }
51
0
      }
52
0
    }
53
1
  }
54
55
1
  num_cvs = 0;
56
1
  for (i = 0; i < op_array->last_var; i++) {
57
0
    if (zend_bitset_in(used_vars, i)) {
58
0
      vars_map[i] = num_cvs++;
59
0
    } else {
60
0
      vars_map[i] = (uint32_t) -1;
61
0
    }
62
0
  }
63
64
1
  num_tmps = 0;
65
1
  for (i = op_array->last_var; i < op_array->last_var + op_array->T; i++) {
66
0
    if (zend_bitset_in(used_vars, i)) {
67
0
      vars_map[i] = num_cvs + num_tmps++;
68
0
    } else {
69
0
      vars_map[i] = (uint32_t) -1;
70
0
    }
71
0
  }
72
73
1
  free_alloca(used_vars, use_heap1);
74
1
  if (num_cvs == op_array->last_var && num_tmps == op_array->T) {
75
1
    free_alloca(vars_map, use_heap2);
76
1
    return;
77
1
  }
78
79
0
  ZEND_ASSERT(num_cvs <= op_array->last_var);
80
0
  ZEND_ASSERT(num_tmps <= op_array->T);
81
82
  /* Update CV and TMP references in opcodes */
83
0
  for (i = 0; i < op_array->last; i++) {
84
0
    zend_op *opline = &op_array->opcodes[i];
85
0
    if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
86
0
      opline->op1.var = NUM_VAR(vars_map[VAR_NUM(opline->op1.var)]);
87
0
    }
88
0
    if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
89
0
      opline->op2.var = NUM_VAR(vars_map[VAR_NUM(opline->op2.var)]);
90
0
    }
91
0
    if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
92
0
      opline->result.var = NUM_VAR(vars_map[VAR_NUM(opline->result.var)]);
93
0
    }
94
0
  }
95
96
  /* Update CV name table */
97
0
  if (num_cvs != op_array->last_var) {
98
0
    if (num_cvs) {
99
0
      zend_string **names = safe_emalloc(sizeof(zend_string *), num_cvs, 0);
100
0
      for (i = 0; i < op_array->last_var; i++) {
101
0
        if (vars_map[i] != (uint32_t) -1) {
102
0
          names[vars_map[i]] = op_array->vars[i];
103
0
        } else {
104
0
          zend_string_release_ex(op_array->vars[i], 0);
105
0
        }
106
0
      }
107
0
      efree(op_array->vars);
108
0
      op_array->vars = names;
109
0
    } else {
110
0
      for (i = 0; i < op_array->last_var; i++) {
111
0
        zend_string_release_ex(op_array->vars[i], 0);
112
0
      }
113
0
      efree(op_array->vars);
114
0
      op_array->vars = NULL;
115
0
    }
116
0
    op_array->last_var = num_cvs;
117
0
  }
118
119
0
  op_array->T = num_tmps + ZEND_OBSERVER_ENABLED; // reserve last temporary for observers if enabled
120
121
  free_alloca(vars_map, use_heap2);
122
0
}