Skip to content

Commit 83b8a89

Browse files
authored
zend_cfg: use uint32_t type rather than int type (#21542)
These are count values and thus should never be negative, more over their use site usually wants them to be uint32_t too. Aligning these types makes the intention clearer. I have also propagated those changes to use sites of these struct members.
1 parent 9cb1150 commit 83b8a89

File tree

18 files changed

+128
-145
lines changed

18 files changed

+128
-145
lines changed

Zend/Optimizer/block_pass.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ static void assemble_code_blocks(const zend_cfg *cfg, zend_op_array *op_array, z
11851185

11861186
/* rebuild map (just for printing) */
11871187
memset(cfg->map, -1, sizeof(int) * op_array->last);
1188-
for (int n = 0; n < cfg->blocks_count; n++) {
1188+
for (uint32_t n = 0; n < cfg->blocks_count; n++) {
11891189
if (cfg->blocks[n].flags & (ZEND_BB_REACHABLE|ZEND_BB_UNREACHABLE_FREE)) {
11901190
cfg->map[cfg->blocks[n].start] = n;
11911191
}
@@ -1493,7 +1493,7 @@ static void zend_jmp_optimization(zend_basic_block *block, zend_op_array *op_arr
14931493
* defined. We won't apply some optimization patterns for such variables. */
14941494
static void zend_t_usage(const zend_cfg *cfg, const zend_op_array *op_array, zend_bitset used_ext, zend_optimizer_ctx *ctx)
14951495
{
1496-
int n;
1496+
uint32_t n;
14971497
zend_basic_block *block, *next_block;
14981498
uint32_t var_num;
14991499
uint32_t bitset_len;
@@ -1697,11 +1697,10 @@ static void zend_t_usage(const zend_cfg *cfg, const zend_op_array *op_array, zen
16971697

16981698
static void zend_merge_blocks(const zend_op_array *op_array, const zend_cfg *cfg, uint32_t *opt_count)
16991699
{
1700-
int i;
17011700
zend_basic_block *b, *bb;
17021701
zend_basic_block *prev = NULL;
17031702

1704-
for (i = 0; i < cfg->blocks_count; i++) {
1703+
for (uint32_t i = 0; i < cfg->blocks_count; i++) {
17051704
b = cfg->blocks + i;
17061705
if (b->flags & ZEND_BB_REACHABLE) {
17071706
if ((b->flags & ZEND_BB_FOLLOW) &&

Zend/Optimizer/dce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer
571571
} FOREACH_PHI_END();
572572

573573
/* Mark reachable instruction without side effects as dead */
574-
int b = ssa->cfg.blocks_count;
574+
uint32_t b = ssa->cfg.blocks_count;
575575
while (b > 0) {
576576
int op_data = -1;
577577

Zend/Optimizer/dfa_pass.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ int zend_dfa_optimize_calls(zend_op_array *op_array, zend_ssa *ssa)
479479
return removed_ops;
480480
}
481481

482-
static zend_always_inline void take_successor_0(zend_ssa *ssa, int block_num, zend_basic_block *block)
482+
static zend_always_inline void take_successor_0(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block)
483483
{
484484
if (block->successors_count == 2) {
485485
if (block->successors[1] != block->successors[0]) {
@@ -489,7 +489,7 @@ static zend_always_inline void take_successor_0(zend_ssa *ssa, int block_num, ze
489489
}
490490
}
491491

492-
static zend_always_inline void take_successor_1(zend_ssa *ssa, int block_num, zend_basic_block *block)
492+
static zend_always_inline void take_successor_1(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block)
493493
{
494494
if (block->successors_count == 2) {
495495
if (block->successors[1] != block->successors[0]) {
@@ -500,11 +500,9 @@ static zend_always_inline void take_successor_1(zend_ssa *ssa, int block_num, ze
500500
}
501501
}
502502

503-
static zend_always_inline void take_successor_ex(zend_ssa *ssa, int block_num, zend_basic_block *block, int target_block)
503+
static zend_always_inline void take_successor_ex(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block, int target_block)
504504
{
505-
int i;
506-
507-
for (i = 0; i < block->successors_count; i++) {
505+
for (uint32_t i = 0; i < block->successors_count; i++) {
508506
if (block->successors[i] != target_block) {
509507
zend_ssa_remove_predecessor(ssa, block_num, block->successors[i]);
510508
}
@@ -531,10 +529,9 @@ static void replace_predecessor(zend_ssa *ssa, int block_id, int old_pred, int n
531529
int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
532530
zend_ssa_phi *phi;
533531

534-
int i;
535532
int old_pred_idx = -1;
536533
int new_pred_idx = -1;
537-
for (i = 0; i < block->predecessors_count; i++) {
534+
for (uint32_t i = 0; i < block->predecessors_count; i++) {
538535
if (predecessors[i] == old_pred) {
539536
old_pred_idx = i;
540537
}
@@ -582,10 +579,9 @@ static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa
582579
zend_basic_block *src = &ssa->cfg.blocks[from];
583580
zend_basic_block *old = &ssa->cfg.blocks[to];
584581
zend_basic_block *dst = &ssa->cfg.blocks[new_to];
585-
int i;
586582
zend_op *opline;
587583

588-
for (i = 0; i < src->successors_count; i++) {
584+
for (uint32_t i = 0; i < src->successors_count; i++) {
589585
if (src->successors[i] == to) {
590586
src->successors[i] = new_to;
591587
}
@@ -650,10 +646,10 @@ static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa
650646
replace_predecessor(ssa, new_to, to, from);
651647
}
652648

653-
static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, int block_num)
649+
static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, uint32_t block_num)
654650
{
655651
if (block->predecessors_count == 1 && ssa->blocks[block_num].phis == NULL) {
656-
int *predecessors, i;
652+
int *predecessors;
657653
zend_basic_block *fe_fetch_block = NULL;
658654

659655
ZEND_ASSERT(block->successors_count == 1);
@@ -669,7 +665,7 @@ static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_b
669665
}
670666
}
671667
}
672-
for (i = 0; i < block->predecessors_count; i++) {
668+
for (uint32_t i = 0; i < block->predecessors_count; i++) {
673669
zend_ssa_replace_control_link(op_array, ssa, predecessors[i], block_num, block->successors[0]);
674670
}
675671
zend_ssa_remove_block(op_array, ssa, block_num);
@@ -686,7 +682,7 @@ static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_b
686682
static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
687683
{
688684
int removed_ops = 0;
689-
int block_num = 0;
685+
uint32_t block_num = 0;
690686

691687
for (block_num = 1; block_num < ssa->cfg.blocks_count; block_num++) {
692688
zend_basic_block *block = &ssa->cfg.blocks[block_num];
@@ -706,7 +702,7 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
706702
block_num++;
707703
}
708704
while (block_num < ssa->cfg.blocks_count) {
709-
int next_block_num = block_num + 1;
705+
uint32_t next_block_num = block_num + 1;
710706
zend_basic_block *block = &ssa->cfg.blocks[block_num];
711707
uint32_t op_num;
712708
zend_op *opline;
@@ -941,11 +937,13 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
941937
if (block_num > 0) {
942938
zend_ssa_unlink_block(op_array, ssa, block, block_num);
943939
/* backtrack to previous basic block */
940+
int backtracking_block_num = block_num;
944941
do {
945-
block_num--;
946-
} while (block_num >= 0
947-
&& !(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE));
948-
if (block_num >= 0) {
942+
backtracking_block_num--;
943+
} while (backtracking_block_num >= 0
944+
&& !(ssa->cfg.blocks[backtracking_block_num].flags & ZEND_BB_REACHABLE));
945+
if (backtracking_block_num >= 0) {
946+
block_num = backtracking_block_num;
949947
continue;
950948
}
951949
}

Zend/Optimizer/escape_analysis.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
7878
zend_ssa_var *ssa_vars = ssa->vars;
7979
int ssa_vars_count = ssa->vars_count;
8080
zend_ssa_phi *p;
81-
int i, j;
81+
int i;
8282
int *size;
8383
ALLOCA_FLAG(use_heap)
8484

@@ -94,7 +94,7 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
9494
if (p->pi >= 0) {
9595
union_find_unite(parent, size, i, p->sources[0]);
9696
} else {
97-
for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
97+
for (uint32_t j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
9898
union_find_unite(parent, size, i, p->sources[j]);
9999
}
100100
}

Zend/Optimizer/sccp.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ static void sccp_mark_feasible_successors(
18321832
zend_op *opline, zend_ssa_op *ssa_op) {
18331833
sccp_ctx *ctx = (sccp_ctx *) scdf;
18341834
zval *op1, zv;
1835-
int s;
1835+
uint32_t s;
18361836

18371837
/* We can't determine the branch target at compile-time for these */
18381838
switch (opline->opcode) {
@@ -2049,7 +2049,6 @@ static void sccp_visit_phi(scdf_ctx *scdf, const zend_ssa_phi *phi) {
20492049
zend_basic_block *block = &ssa->cfg.blocks[phi->block];
20502050
int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
20512051

2052-
int i;
20532052
zval result;
20542053
MAKE_TOP(&result);
20552054
#if SCP_DEBUG
@@ -2061,7 +2060,7 @@ static void sccp_visit_phi(scdf_ctx *scdf, const zend_ssa_phi *phi) {
20612060
join_phi_values(&result, &ctx->values[phi->sources[0]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
20622061
}
20632062
} else {
2064-
for (i = 0; i < block->predecessors_count; i++) {
2063+
for (uint32_t i = 0; i < block->predecessors_count; i++) {
20652064
ZEND_ASSERT(phi->sources[i] >= 0);
20662065
if (scdf_is_edge_feasible(scdf, predecessors[i], phi->block)) {
20672066
#if SCP_DEBUG

Zend/Optimizer/scdf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,8 @@ static uint32_t cleanup_loop_var_free_block(const scdf_ctx *scdf, const zend_bas
254254
* unreachable. Blocks already marked unreachable are not removed. */
255255
uint32_t scdf_remove_unreachable_blocks(const scdf_ctx *scdf) {
256256
zend_ssa *ssa = scdf->ssa;
257-
int i;
258257
uint32_t removed_ops = 0;
259-
for (i = 0; i < ssa->cfg.blocks_count; i++) {
258+
for (uint32_t i = 0; i < ssa->cfg.blocks_count; i++) {
260259
const zend_basic_block *block = &ssa->cfg.blocks[i];
261260
if (!zend_bitset_in(scdf->executable_blocks, i) && (block->flags & ZEND_BB_REACHABLE)) {
262261
if (!kept_alive_by_loop_var_free(scdf, block)) {

Zend/Optimizer/scdf.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ static inline void scdf_add_def_to_worklist(const scdf_ctx *scdf, int var_num) {
7575
}
7676
}
7777

78-
static inline uint32_t scdf_edge(const zend_cfg *cfg, int from, int to) {
78+
static inline uint32_t scdf_edge(const zend_cfg *cfg, int from, uint32_t to) {
7979
const zend_basic_block *to_block = cfg->blocks + to;
80-
int i;
8180

82-
for (i = 0; i < to_block->predecessors_count; i++) {
81+
for (uint32_t i = 0; i < to_block->predecessors_count; i++) {
8382
uint32_t edge = to_block->predecessor_offset + i;
8483

8584
if (cfg->predecessors[edge] == from) {
@@ -89,7 +88,7 @@ static inline uint32_t scdf_edge(const zend_cfg *cfg, int from, int to) {
8988
ZEND_UNREACHABLE();
9089
}
9190

92-
static inline bool scdf_is_edge_feasible(const scdf_ctx *scdf, int from, int to) {
91+
static inline bool scdf_is_edge_feasible(const scdf_ctx *scdf, int from, uint32_t to) {
9392
uint32_t edge = scdf_edge(&scdf->ssa->cfg, from, to);
9493
return zend_bitset_in(scdf->feasible_edges, edge);
9594
}

Zend/Optimizer/ssa_integrity.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ static inline bool is_in_phi_sources(zend_ssa *ssa, zend_ssa_phi *phi, int check
6666
}
6767

6868
static inline bool is_in_predecessors(zend_cfg *cfg, zend_basic_block *block, int check) {
69-
int i, *predecessors = &cfg->predecessors[block->predecessor_offset];
70-
for (i = 0; i < block->predecessors_count; i++) {
69+
int *predecessors = &cfg->predecessors[block->predecessor_offset];
70+
for (uint32_t i = 0; i < block->predecessors_count; i++) {
7171
if (predecessors[i] == check) {
7272
return true;
7373
}
@@ -76,8 +76,7 @@ static inline bool is_in_predecessors(zend_cfg *cfg, zend_basic_block *block, in
7676
}
7777

7878
static inline bool is_in_successors(zend_basic_block *block, int check) {
79-
int s;
80-
for (s = 0; s < block->successors_count; s++) {
79+
for (uint32_t s = 0; s < block->successors_count; s++) {
8180
if (block->successors[s] == check) {
8281
return true;
8382
}
@@ -329,7 +328,7 @@ void ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *ex
329328

330329
/* Phis */
331330
FOREACH_PHI(phi) {
332-
unsigned num_sources = NUM_PHI_SOURCES(phi);
331+
uint32_t num_sources = NUM_PHI_SOURCES(phi);
333332
for (i = 0; i < num_sources; i++) {
334333
int source = phi->sources[i];
335334
if (source < 0) {
@@ -360,7 +359,7 @@ void ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *ex
360359
for (i = 0; i < cfg->blocks_count; i++) {
361360
zend_basic_block *block = &cfg->blocks[i];
362361
int *predecessors = &cfg->predecessors[block->predecessor_offset];
363-
int s, j;
362+
uint32_t j;
364363

365364
if (i != 0 && block->start < (block-1)->start + (block-1)->len) {
366365
FAIL("Block %d start %d smaller previous end %d\n",
@@ -384,7 +383,7 @@ void ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *ex
384383
continue;
385384
}
386385

387-
for (s = 0; s < block->successors_count; s++) {
386+
for (uint32_t s = 0; s < block->successors_count; s++) {
388387
zend_basic_block *next_block;
389388
if (block->successors[s] < 0) {
390389
FAIL("Successor number %d of %d negative", s, i);
@@ -400,15 +399,14 @@ void ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *ex
400399

401400
for (j = 0; j < block->predecessors_count; j++) {
402401
if (predecessors[j] >= 0) {
403-
int k;
404402
zend_basic_block *prev_block = &cfg->blocks[predecessors[j]];
405403
if (!(prev_block->flags & ZEND_BB_REACHABLE)) {
406404
FAIL("Predecessor %d of %d not reachable\n", predecessors[j], i);
407405
}
408406
if (!is_in_successors(prev_block, i)) {
409407
FAIL("Block %d successors missing %d\n", predecessors[j], i);
410408
}
411-
for (k = 0; k < block->predecessors_count; k++) {
409+
for (uint32_t k = 0; k < block->predecessors_count; k++) {
412410
if (k != j && predecessors[k] == predecessors[j]) {
413411
FAIL("Block %d has duplicate predecessor %d\n", i, predecessors[j]);
414412
}

0 commit comments

Comments
 (0)