Skip to content

Commit 558856e

Browse files
committed
Optimizer: add const qualifiers
1 parent 769441b commit 558856e

13 files changed

Lines changed: 120 additions & 123 deletions

Zend/Optimizer/block_pass.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ static void zend_merge_blocks(const zend_op_array *op_array, const zend_cfg *cfg
17021702
void zend_optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx)
17031703
{
17041704
zend_cfg cfg;
1705-
zend_basic_block *blocks, *end, *b;
1705+
zend_basic_block *blocks, *b;
17061706
int pass;
17071707
uint32_t bitset_len;
17081708
zend_bitset usage;
@@ -1730,7 +1730,7 @@ void zend_optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx)
17301730
jmp_hitlist = zend_arena_alloc(&ctx->arena, cfg.blocks_count * sizeof(int));
17311731

17321732
blocks = cfg.blocks;
1733-
end = blocks + cfg.blocks_count;
1733+
const zend_basic_block *end = blocks + cfg.blocks_count;
17341734
for (pass = 0; pass < PASSES; pass++) {
17351735
opt_count = 0;
17361736

Zend/Optimizer/compact_literals.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ typedef struct _literal_info {
4141
info[n].num_related = (related); \
4242
} while (0)
4343

44-
static uint32_t add_static_slot(HashTable *hash,
45-
zend_op_array *op_array,
46-
uint32_t op1,
47-
uint32_t op2,
48-
uint32_t kind,
49-
uint32_t *cache_size)
50-
{
44+
static uint32_t add_static_slot(
45+
HashTable *hash,
46+
const zend_op_array *op_array,
47+
uint32_t op1,
48+
uint32_t op2,
49+
uint32_t kind,
50+
uint32_t *cache_size
51+
) {
5152
uint32_t ret;
52-
zval *class_name = &op_array->literals[op1];
53-
zval *prop_name = &op_array->literals[op2];
54-
zval *pos, tmp;
53+
const zval *class_name = &op_array->literals[op1];
54+
const zval *prop_name = &op_array->literals[op2];
5555

5656
zend_string *key = zend_create_member_string(Z_STR_P(class_name), Z_STR_P(prop_name));
5757
ZSTR_H(key) = zend_string_hash_func(key);
5858
ZSTR_H(key) += kind;
5959

60-
pos = zend_hash_find(hash, key);
60+
const zval *pos = zend_hash_find(hash, key);
6161
if (pos) {
6262
ret = Z_LVAL_P(pos);
6363
} else {
64+
zval tmp;
6465
ret = *cache_size;
6566
*cache_size += (kind == LITERAL_STATIC_PROPERTY ? 3 : 2) * sizeof(void *);
6667
ZVAL_LONG(&tmp, ret);
@@ -77,7 +78,7 @@ static inline void bias_key(zend_string *key, uint32_t bias)
7778
ZSTR_H(key) = zend_string_hash_val(key) + bias;
7879
}
7980

80-
static zend_string *create_str_cache_key(zval *literal, uint8_t num_related)
81+
static zend_string *create_str_cache_key(const zval *literal, uint8_t num_related)
8182
{
8283
ZEND_ASSERT(Z_TYPE_P(literal) == IS_STRING);
8384
if (num_related == 1) {

Zend/Optimizer/compact_vars.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void zend_optimizer_compact_vars(zend_op_array *op_array) {
3333
/* Determine which CVs are used */
3434
zend_bitset_clear(used_vars, used_vars_len);
3535
for (i = 0; i < op_array->last; i++) {
36-
zend_op *opline = &op_array->opcodes[i];
36+
const zend_op *opline = &op_array->opcodes[i];
3737
if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
3838
zend_bitset_incl(used_vars, VAR_NUM(opline->op1.var));
3939
}

Zend/Optimizer/dfa_pass.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ zend_result zend_dfa_analyze_op_array(zend_op_array *op_array, zend_optimizer_ct
108108
return SUCCESS;
109109
}
110110

111-
static void zend_ssa_remove_nops(zend_op_array *op_array, zend_ssa *ssa, zend_optimizer_ctx *ctx)
111+
static void zend_ssa_remove_nops(zend_op_array *op_array, const zend_ssa *ssa, zend_optimizer_ctx *ctx)
112112
{
113113
zend_basic_block *blocks = ssa->cfg.blocks;
114114
zend_basic_block *blocks_end = blocks + ssa->cfg.blocks_count;
@@ -290,9 +290,9 @@ static inline bool can_elide_list_type(
290290
}
291291

292292
static inline bool can_elide_return_type_check(
293-
const zend_script *script, zend_op_array *op_array, zend_ssa *ssa, zend_ssa_op *ssa_op) {
294-
zend_arg_info *arg_info = &op_array->arg_info[-1];
295-
zend_ssa_var_info *use_info = &ssa->var_info[ssa_op->op1_use];
293+
const zend_script *script, const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) {
294+
const zend_arg_info *arg_info = &op_array->arg_info[-1];
295+
const zend_ssa_var_info *use_info = &ssa->var_info[ssa_op->op1_use];
296296
uint32_t use_type = use_info->type & (MAY_BE_ANY|MAY_BE_UNDEF);
297297
if (use_type & MAY_BE_REF) {
298298
return false;
@@ -317,7 +317,7 @@ static inline bool can_elide_return_type_check(
317317
}
318318

319319
static bool opline_supports_assign_contraction(
320-
zend_op_array *op_array, zend_ssa *ssa, zend_op *opline, int src_var, uint32_t cv_var) {
320+
const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, int src_var, uint32_t cv_var) {
321321
if (opline->opcode == ZEND_NEW) {
322322
/* see Zend/tests/generators/aborted_yield_during_new.phpt */
323323
return false;
@@ -378,7 +378,7 @@ static bool opline_supports_assign_contraction(
378378
return true;
379379
}
380380

381-
static bool variable_defined_or_used_in_range(zend_ssa *ssa, int var, int start, int end)
381+
static bool variable_defined_or_used_in_range(const zend_ssa *ssa, int var, int start, int end)
382382
{
383383
while (start < end) {
384384
const zend_ssa_op *ssa_op = &ssa->ops[start];
@@ -571,11 +571,11 @@ static void replace_predecessor(zend_ssa *ssa, int block_id, int old_pred, int n
571571
}
572572
}
573573

574-
static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa, int from, int to, int new_to)
574+
static void zend_ssa_replace_control_link(const zend_op_array *op_array, zend_ssa *ssa, int from, int to, int new_to)
575575
{
576-
zend_basic_block *src = &ssa->cfg.blocks[from];
577-
zend_basic_block *old = &ssa->cfg.blocks[to];
578-
zend_basic_block *dst = &ssa->cfg.blocks[new_to];
576+
const zend_basic_block *src = &ssa->cfg.blocks[from];
577+
const zend_basic_block *old = &ssa->cfg.blocks[to];
578+
const zend_basic_block *dst = &ssa->cfg.blocks[new_to];
579579
zend_op *opline;
580580

581581
for (uint32_t i = 0; i < src->successors_count; i++) {
@@ -643,7 +643,7 @@ static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa
643643
replace_predecessor(ssa, new_to, to, from);
644644
}
645645

646-
static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, uint32_t block_num)
646+
static void zend_ssa_unlink_block(const zend_op_array *op_array, zend_ssa *ssa, const zend_basic_block *block, uint32_t block_num)
647647
{
648648
if (block->predecessors_count == 1 && ssa->blocks[block_num].phis == NULL) {
649649
int *predecessors;
@@ -837,7 +837,7 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
837837
break;
838838
case ZEND_COALESCE:
839839
{
840-
zend_ssa_var *var = &ssa->vars[ssa_op->result_def];
840+
const zend_ssa_var *var = &ssa->vars[ssa_op->result_def];
841841
if (opline->op1_type == IS_CONST
842842
&& var->use_chain < 0 && var->phi_use_chain == NULL) {
843843
if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op1.constant)) == IS_NULL) {
@@ -859,7 +859,7 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
859859
}
860860
case ZEND_JMP_NULL:
861861
{
862-
zend_ssa_var *var = &ssa->vars[ssa_op->result_def];
862+
const zend_ssa_var *var = &ssa->vars[ssa_op->result_def];
863863
if (opline->op1_type == IS_CONST
864864
&& var->use_chain < 0 && var->phi_use_chain == NULL) {
865865
if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op1.constant)) == IS_NULL) {
@@ -904,8 +904,8 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
904904

905905
uint32_t target;
906906
if (correct_type) {
907-
HashTable *jmptable = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant));
908-
zval *jmp_zv = type == IS_LONG
907+
const HashTable *jmptable = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant));
908+
const zval *jmp_zv = type == IS_LONG
909909
? zend_hash_index_find(jmptable, Z_LVAL_P(zv))
910910
: zend_hash_find(jmptable, Z_STR_P(zv));
911911

@@ -957,7 +957,7 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
957957
return removed_ops;
958958
}
959959

960-
static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ssa, int def, int cv_var)
960+
static bool zend_dfa_try_to_replace_result(const zend_op_array *op_array, const zend_ssa *ssa, int def, int cv_var)
961961
{
962962
int result_var = ssa->ops[def].result_def;
963963
uint32_t cv = EX_NUM_TO_VAR(ssa->vars[cv_var].var);

Zend/Optimizer/escape_analysis.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ static zend_always_inline void union_find_unite(int *parent, int *size, int i, i
7171
}
7272
/* }}} */
7373

74-
static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
74+
static zend_result zend_build_equi_escape_sets(int *parent, const zend_op_array *op_array, const zend_ssa *ssa) /* {{{ */
7575
{
7676
zend_ssa_var *ssa_vars = ssa->vars;
7777
int ssa_vars_count = ssa->vars_count;
78-
zend_ssa_phi *p;
7978
int i;
8079
int *size;
8180
ALLOCA_FLAG(use_heap)
@@ -88,7 +87,7 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
8887

8988
for (i = 0; i < ssa_vars_count; i++) {
9089
if (ssa_vars[i].definition_phi) {
91-
p = ssa_vars[i].definition_phi;
90+
const zend_ssa_phi *p = ssa_vars[i].definition_phi;
9291
if (p->pi >= 0) {
9392
union_find_unite(parent, size, i, p->sources[0]);
9493
} else {
@@ -98,8 +97,8 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
9897
}
9998
} else if (ssa_vars[i].definition >= 0) {
10099
int def = ssa_vars[i].definition;
101-
zend_ssa_op *op = ssa->ops + def;
102-
zend_op *opline = op_array->opcodes + def;
100+
const zend_ssa_op *op = ssa->ops + def;
101+
const zend_op *opline = op_array->opcodes + def;
103102

104103
if (op->op1_def >= 0) {
105104
if (op->op1_use >= 0) {
@@ -145,9 +144,9 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
145144
}
146145
/* }}} */
147146

148-
static bool is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
147+
static bool is_allocation_def(const zend_op_array *op_array, const zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
149148
{
150-
zend_ssa_op *ssa_op = ssa->ops + def;
149+
const zend_ssa_op *ssa_op = ssa->ops + def;
151150
zend_op *opline = op_array->opcodes + def;
152151

153152
if (ssa_op->result_def == var) {
@@ -156,7 +155,7 @@ static bool is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, i
156155
return true;
157156
case ZEND_NEW: {
158157
/* objects with destructors should escape */
159-
zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
158+
const zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
160159
script, op_array, opline);
161160
uint32_t forbidden_flags =
162161
/* These flags will always cause an exception */
@@ -216,10 +215,10 @@ static bool is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, i
216215
}
217216
/* }}} */
218217

219-
static bool is_local_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
218+
static bool is_local_def(const zend_op_array *op_array, const zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
220219
{
221-
zend_ssa_op *op = ssa->ops + def;
222-
zend_op *opline = op_array->opcodes + def;
220+
const zend_ssa_op *op = ssa->ops + def;
221+
const zend_op *opline = op_array->opcodes + def;
223222

224223
if (op->result_def == var) {
225224
switch (opline->opcode) {
@@ -230,7 +229,7 @@ static bool is_local_def(zend_op_array *op_array, zend_ssa *ssa, int def, int va
230229
return true;
231230
case ZEND_NEW: {
232231
/* objects with destructors should escape */
233-
zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
232+
const zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
234233
script, op_array, opline);
235234
if (ce
236235
&& !ce->create_object
@@ -266,10 +265,10 @@ static bool is_local_def(zend_op_array *op_array, zend_ssa *ssa, int def, int va
266265
}
267266
/* }}} */
268267

269-
static bool is_escape_use(zend_op_array *op_array, zend_ssa *ssa, int use, int var) /* {{{ */
268+
static bool is_escape_use(const zend_op_array *op_array, const zend_ssa *ssa, int use, int var) /* {{{ */
270269
{
271-
zend_ssa_op *ssa_op = ssa->ops + use;
272-
zend_op *opline = op_array->opcodes + use;
270+
const zend_ssa_op *ssa_op = ssa->ops + use;
271+
const zend_op *opline = op_array->opcodes + use;
273272

274273
if (ssa_op->op1_use == var) {
275274
switch (opline->opcode) {
@@ -377,7 +376,7 @@ static bool is_escape_use(zend_op_array *op_array, zend_ssa *ssa, int use, int v
377376
}
378377
/* }}} */
379378

380-
zend_result zend_ssa_escape_analysis(const zend_script *script, zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
379+
zend_result zend_ssa_escape_analysis(const zend_script *script, const zend_op_array *op_array, const zend_ssa *ssa) /* {{{ */
381380
{
382381
zend_ssa_var *ssa_vars = ssa->vars;
383382
int ssa_vars_count = ssa->vars_count;
@@ -474,8 +473,8 @@ zend_result zend_ssa_escape_analysis(const zend_script *script, zend_op_array *o
474473
root = ees[i];
475474
if (ssa_vars[root].escape_state == ESCAPE_STATE_NO_ESCAPE) {
476475
FOREACH_USE(ssa_vars + i, use) {
477-
zend_ssa_op *op = ssa->ops + use;
478-
zend_op *opline = op_array->opcodes + use;
476+
const zend_ssa_op *op = ssa->ops + use;
477+
const zend_op *opline = op_array->opcodes + use;
479478
int enclosing_root;
480479

481480
if (opline->opcode == ZEND_OP_DATA &&

Zend/Optimizer/optimize_temp_vars_5.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_c
108108
opline->opcode == ZEND_RETURN_BY_REF ||
109109
opline->opcode == ZEND_FREE ||
110110
opline->opcode == ZEND_FE_FREE)) {
111-
zend_op *curr = opline;
111+
const zend_op *curr = opline;
112112

113113
while (--curr >= end) {
114114
if (curr->opcode == ZEND_FAST_CALL) {

Zend/Optimizer/pass3.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "zend_vm.h"
3030

3131
/* 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)
32+
static zend_always_inline bool in_hitlist(const zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
3333
{
3434
int i;
3535

@@ -51,15 +51,14 @@ static zend_always_inline bool in_hitlist(zend_op *target, zend_op **jmp_hitlist
5151
void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
5252
{
5353
zend_op *opline;
54-
zend_op *end;
5554
zend_op *target;
5655
zend_op **jmp_hitlist;
5756
int jmp_hitlist_count;
5857
ALLOCA_FLAG(use_heap);
5958

6059
jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
6160
opline = op_array->opcodes;
62-
end = opline + op_array->last;
61+
const zend_op *end = opline + op_array->last;
6362

6463
while (opline < end) {
6564

0 commit comments

Comments
 (0)