Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Zend/Optimizer/block_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ static void assemble_code_blocks(const zend_cfg *cfg, zend_op_array *op_array, z

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

static void zend_merge_blocks(const zend_op_array *op_array, const zend_cfg *cfg, uint32_t *opt_count)
{
int i;
zend_basic_block *b, *bb;
zend_basic_block *prev = NULL;

for (i = 0; i < cfg->blocks_count; i++) {
for (uint32_t i = 0; i < cfg->blocks_count; i++) {
b = cfg->blocks + i;
if (b->flags & ZEND_BB_REACHABLE) {
if ((b->flags & ZEND_BB_FOLLOW) &&
Expand Down
2 changes: 1 addition & 1 deletion Zend/Optimizer/dce.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer
} FOREACH_PHI_END();

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

Expand Down
36 changes: 17 additions & 19 deletions Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ int zend_dfa_optimize_calls(zend_op_array *op_array, zend_ssa *ssa)
return removed_ops;
}

static zend_always_inline void take_successor_0(zend_ssa *ssa, int block_num, zend_basic_block *block)
static zend_always_inline void take_successor_0(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block)
{
if (block->successors_count == 2) {
if (block->successors[1] != block->successors[0]) {
Expand All @@ -489,7 +489,7 @@ static zend_always_inline void take_successor_0(zend_ssa *ssa, int block_num, ze
}
}

static zend_always_inline void take_successor_1(zend_ssa *ssa, int block_num, zend_basic_block *block)
static zend_always_inline void take_successor_1(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block)
{
if (block->successors_count == 2) {
if (block->successors[1] != block->successors[0]) {
Expand All @@ -500,11 +500,9 @@ static zend_always_inline void take_successor_1(zend_ssa *ssa, int block_num, ze
}
}

static zend_always_inline void take_successor_ex(zend_ssa *ssa, int block_num, zend_basic_block *block, int target_block)
static zend_always_inline void take_successor_ex(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block, int target_block)
{
int i;

for (i = 0; i < block->successors_count; i++) {
for (uint32_t i = 0; i < block->successors_count; i++) {
if (block->successors[i] != target_block) {
zend_ssa_remove_predecessor(ssa, block_num, block->successors[i]);
}
Expand All @@ -531,10 +529,9 @@ static void replace_predecessor(zend_ssa *ssa, int block_id, int old_pred, int n
int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
zend_ssa_phi *phi;

int i;
int old_pred_idx = -1;
int new_pred_idx = -1;
for (i = 0; i < block->predecessors_count; i++) {
for (uint32_t i = 0; i < block->predecessors_count; i++) {
if (predecessors[i] == old_pred) {
old_pred_idx = i;
}
Expand Down Expand Up @@ -582,10 +579,9 @@ static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa
zend_basic_block *src = &ssa->cfg.blocks[from];
zend_basic_block *old = &ssa->cfg.blocks[to];
zend_basic_block *dst = &ssa->cfg.blocks[new_to];
int i;
zend_op *opline;

for (i = 0; i < src->successors_count; i++) {
for (uint32_t i = 0; i < src->successors_count; i++) {
if (src->successors[i] == to) {
src->successors[i] = new_to;
}
Expand Down Expand Up @@ -650,10 +646,10 @@ static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa
replace_predecessor(ssa, new_to, to, from);
}

static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, int block_num)
static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, uint32_t block_num)
{
if (block->predecessors_count == 1 && ssa->blocks[block_num].phis == NULL) {
int *predecessors, i;
int *predecessors;
zend_basic_block *fe_fetch_block = NULL;

ZEND_ASSERT(block->successors_count == 1);
Expand All @@ -669,7 +665,7 @@ static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_b
}
}
}
for (i = 0; i < block->predecessors_count; i++) {
for (uint32_t i = 0; i < block->predecessors_count; i++) {
zend_ssa_replace_control_link(op_array, ssa, predecessors[i], block_num, block->successors[0]);
}
zend_ssa_remove_block(op_array, ssa, block_num);
Expand All @@ -686,7 +682,7 @@ static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_b
static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
{
int removed_ops = 0;
int block_num = 0;
uint32_t block_num = 0;

for (block_num = 1; block_num < ssa->cfg.blocks_count; block_num++) {
zend_basic_block *block = &ssa->cfg.blocks[block_num];
Expand All @@ -706,7 +702,7 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
block_num++;
}
while (block_num < ssa->cfg.blocks_count) {
int next_block_num = block_num + 1;
uint32_t next_block_num = block_num + 1;
zend_basic_block *block = &ssa->cfg.blocks[block_num];
uint32_t op_num;
zend_op *opline;
Expand Down Expand Up @@ -941,11 +937,13 @@ static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa)
if (block_num > 0) {
zend_ssa_unlink_block(op_array, ssa, block, block_num);
/* backtrack to previous basic block */
int backtracking_block_num = block_num;
do {
block_num--;
} while (block_num >= 0
&& !(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE));
if (block_num >= 0) {
backtracking_block_num--;
} while (backtracking_block_num >= 0
&& !(ssa->cfg.blocks[backtracking_block_num].flags & ZEND_BB_REACHABLE));
if (backtracking_block_num >= 0) {
block_num = backtracking_block_num;
continue;
Comment on lines +940 to 947
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that there should be a better way to solve this but when I initially did:

Suggested change
int backtracking_block_num = block_num;
do {
block_num--;
} while (block_num >= 0
backtracking_block_num--;
} while (backtracking_block_num >= 0
&& !(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE));
if (block_num >= 0) {
if (backtracking_block_num >= 0) {
continue;
do {
block_num--;
} while (block_num > 0
&& !(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE));
if (!(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE)) {
continue;

it didn't behave as expected, so I guess I'm not fully grasping what is happening here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you had inverted the logic in if (!(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE)) {. Before, if (block_num >= 0) implied that ssa->cfg.blocks[block_num] was reachable. So it should be if ((ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE)) {.

But I find the original logic easier to follow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to keep the original logic, although I might add a comment explaining what is happening :)

}
}
Expand Down
4 changes: 2 additions & 2 deletions Zend/Optimizer/escape_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
zend_ssa_var *ssa_vars = ssa->vars;
int ssa_vars_count = ssa->vars_count;
zend_ssa_phi *p;
int i, j;
int i;
int *size;
ALLOCA_FLAG(use_heap)

Expand All @@ -94,7 +94,7 @@ static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_ar
if (p->pi >= 0) {
union_find_unite(parent, size, i, p->sources[0]);
} else {
for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
for (uint32_t j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
union_find_unite(parent, size, i, p->sources[j]);
}
}
Expand Down
5 changes: 2 additions & 3 deletions Zend/Optimizer/sccp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ static void sccp_mark_feasible_successors(
zend_op *opline, zend_ssa_op *ssa_op) {
sccp_ctx *ctx = (sccp_ctx *) scdf;
zval *op1, zv;
int s;
uint32_t s;

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

int i;
zval result;
MAKE_TOP(&result);
#if SCP_DEBUG
Expand All @@ -2061,7 +2060,7 @@ static void sccp_visit_phi(scdf_ctx *scdf, const zend_ssa_phi *phi) {
join_phi_values(&result, &ctx->values[phi->sources[0]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
}
} else {
for (i = 0; i < block->predecessors_count; i++) {
for (uint32_t i = 0; i < block->predecessors_count; i++) {
ZEND_ASSERT(phi->sources[i] >= 0);
if (scdf_is_edge_feasible(scdf, predecessors[i], phi->block)) {
#if SCP_DEBUG
Expand Down
3 changes: 1 addition & 2 deletions Zend/Optimizer/scdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ static uint32_t cleanup_loop_var_free_block(const scdf_ctx *scdf, const zend_bas
* unreachable. Blocks already marked unreachable are not removed. */
uint32_t scdf_remove_unreachable_blocks(const scdf_ctx *scdf) {
zend_ssa *ssa = scdf->ssa;
int i;
uint32_t removed_ops = 0;
for (i = 0; i < ssa->cfg.blocks_count; i++) {
for (uint32_t i = 0; i < ssa->cfg.blocks_count; i++) {
const zend_basic_block *block = &ssa->cfg.blocks[i];
if (!zend_bitset_in(scdf->executable_blocks, i) && (block->flags & ZEND_BB_REACHABLE)) {
if (!kept_alive_by_loop_var_free(scdf, block)) {
Expand Down
7 changes: 3 additions & 4 deletions Zend/Optimizer/scdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ static inline void scdf_add_def_to_worklist(const scdf_ctx *scdf, int var_num) {
}
}

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

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

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

static inline bool scdf_is_edge_feasible(const scdf_ctx *scdf, int from, int to) {
static inline bool scdf_is_edge_feasible(const scdf_ctx *scdf, int from, uint32_t to) {
uint32_t edge = scdf_edge(&scdf->ssa->cfg, from, to);
return zend_bitset_in(scdf->feasible_edges, edge);
}
Expand Down
16 changes: 7 additions & 9 deletions Zend/Optimizer/ssa_integrity.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ static inline bool is_in_phi_sources(zend_ssa *ssa, zend_ssa_phi *phi, int check
}

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

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

/* Phis */
FOREACH_PHI(phi) {
unsigned num_sources = NUM_PHI_SOURCES(phi);
uint32_t num_sources = NUM_PHI_SOURCES(phi);
for (i = 0; i < num_sources; i++) {
int source = phi->sources[i];
if (source < 0) {
Expand Down Expand Up @@ -360,7 +359,7 @@ void ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *ex
for (i = 0; i < cfg->blocks_count; i++) {
zend_basic_block *block = &cfg->blocks[i];
int *predecessors = &cfg->predecessors[block->predecessor_offset];
int s, j;
uint32_t j;

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

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

for (j = 0; j < block->predecessors_count; j++) {
if (predecessors[j] >= 0) {
int k;
zend_basic_block *prev_block = &cfg->blocks[predecessors[j]];
if (!(prev_block->flags & ZEND_BB_REACHABLE)) {
FAIL("Predecessor %d of %d not reachable\n", predecessors[j], i);
}
if (!is_in_successors(prev_block, i)) {
FAIL("Block %d successors missing %d\n", predecessors[j], i);
}
for (k = 0; k < block->predecessors_count; k++) {
for (uint32_t k = 0; k < block->predecessors_count; k++) {
if (k != j && predecessors[k] == predecessors[j]) {
FAIL("Block %d has duplicate predecessor %d\n", i, predecessors[j]);
}
Expand Down
Loading
Loading