[php-src] master: Update IR
| From: | Dmitry Stogov | Date: | Wed, 20 Mar 2024 10:37:03 +0000 |
| Subject: | [php-src] master: Update IR | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127807@lists.php.net to get a copy of this message | ||
Author: Dmitry Stogov (dstogov)
Date: 2024-03-20T13:22:11+03:00
Commit: https://github.com/php/php-src/commit/eb513503c02d1041b005f564ac544f7189461e86
Raw diff: https://github.com/php/php-src/commit/eb513503c02d1041b005f564ac544f7189461e86.diff
Update IR
IR commit: 0e3608e3c945140d366134034ee8d86edbe6d050
Changed paths:
M ext/opcache/jit/ir/gen_ir_fold_hash.c
M ext/opcache/jit/ir/ir.h
M ext/opcache/jit/ir/ir_dump.c
M ext/opcache/jit/ir/ir_fold.h
M ext/opcache/jit/ir/ir_save.c
M ext/opcache/jit/ir/ir_x86.dasc
Diff:
diff --git a/ext/opcache/jit/ir/gen_ir_fold_hash.c b/ext/opcache/jit/ir/gen_ir_fold_hash.c
index b55594690249..800da27bdfa9 100644
--- a/ext/opcache/jit/ir/gen_ir_fold_hash.c
+++ b/ext/opcache/jit/ir/gen_ir_fold_hash.c
@@ -255,6 +255,9 @@ int main(int argc, char **argv)
IR_OPS(IR_OP_ADD)
+ ir_strtab_lookup(&strtab, "C_INTPTR", sizeof("C_INTPTR") - 1, IR_C_INTPTR
+ 1);
+ ir_strtab_lookup(&strtab, "C_UINTPTR", sizeof("C_IINTPTR") - 1,
IR_C_UINTPTR + 1);
+
while (fgets(buf, sizeof(buf) - 1, f)) {
size_t len = strlen(buf);
if (len > 0 && (buf[len - 1] == '\r' || buf[len - 1] == '\n')) {
diff --git a/ext/opcache/jit/ir/ir.h b/ext/opcache/jit/ir/ir.h
index 14ebc942e6e4..16551b3bafa4 100644
--- a/ext/opcache/jit/ir/ir.h
+++ b/ext/opcache/jit/ir/ir.h
@@ -156,11 +156,15 @@ typedef enum _ir_type {
# define IR_SSIZE_T IR_I64
# define IR_UINTPTR_T IR_U64
# define IR_INTPTR_T IR_I64
+# define IR_C_UINTPTR IR_U64
+# define IR_C_INTPTR IR_I64
#else
# define IR_SIZE_T IR_U32
# define IR_SSIZE_T IR_I32
# define IR_UINTPTR_T IR_U32
# define IR_INTPTR_T IR_I32
+# define IR_C_UINTPTR IR_U32
+# define IR_C_INTPTR IR_I32
#endif
/* List of IR opcodes
@@ -839,8 +843,14 @@ int ir_load_llvm_bitcode(ir_loader *loader, const char *filename);
int ir_load_llvm_asm(ir_loader *loader, const char *filename);
/* IR save API (implementation in ir_save.c) */
+#define IR_SAVE_CFG (1<<0) /* add info about CFG */
+#define IR_SAVE_CFG_MAP (1<<1) /* add info about CFG block assignment */
+#define IR_SAVE_USE_LISTS (1<<2) /* add info about def->use lists */
+#define IR_SAVE_RULES (1<<3) /* add info about selected code-generation rules */
+#define IR_SAVE_REGS (1<<4) /* add info about selected registers */
+
void ir_print_proto(const ir_ctx *ctx, ir_ref proto, FILE *f);
-void ir_save(const ir_ctx *ctx, FILE *f);
+void ir_save(const ir_ctx *ctx, uint32_t save_flags, FILE *f);
/* IR debug dump API (implementation in ir_dump.c) */
void ir_dump(const ir_ctx *ctx, FILE *f);
diff --git a/ext/opcache/jit/ir/ir_dump.c b/ext/opcache/jit/ir/ir_dump.c
index 1fac35788726..c3a86e252229 100644
--- a/ext/opcache/jit/ir/ir_dump.c
+++ b/ext/opcache/jit/ir/ir_dump.c
@@ -511,7 +511,46 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f)
if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) {
continue;
}
- fprintf(f, "#BB%d:\n", b);
+
+ fprintf(f, "#BB%d: end=l_%d", b, bb->end);
+ if (bb->flags && IR_BB_UNREACHABLE) {
+ fprintf(f, ", U");
+ }
+ if (bb->dom_parent > 0) {
+ fprintf(f, ", idom=BB%d(%d)", bb->dom_parent, bb->dom_depth);
+ }
+ if (bb->loop_depth != 0) {
+ if (bb->flags & IR_BB_LOOP_HEADER) {
+ if (bb->loop_header > 0) {
+ fprintf(f, ", loop=HDR,BB%d(%d)", bb->loop_header, bb->loop_depth);
+ } else {
+ IR_ASSERT(bb->loop_depth == 1);
+ fprintf(f, ", loop=HDR(%d)", bb->loop_depth);
+ }
+ } else {
+ IR_ASSERT(bb->loop_header > 0);
+ fprintf(f, ", loop=BB%d(%d)", bb->loop_header, bb->loop_depth);
+ }
+ }
+ if (bb->predecessors_count) {
+ uint32_t i;
+
+ fprintf(f, ", pred(%d)=[BB%d", bb->predecessors_count,
ctx->cfg_edges[bb->predecessors]);
+ for (i = 1; i < bb->predecessors_count; i++) {
+ fprintf(f, ", BB%d", ctx->cfg_edges[bb->predecessors + i]);
+ }
+ fprintf(f, "]");
+ }
+ if (bb->successors_count) {
+ uint32_t i;
+
+ fprintf(f, ", succ(%d)=[BB%d", bb->successors_count,
ctx->cfg_edges[bb->successors]);
+ for (i = 1; i < bb->successors_count; i++) {
+ fprintf(f, ", BB%d", ctx->cfg_edges[bb->successors + i]);
+ }
+ fprintf(f, "]");
+ }
+ fprintf(f, "\n");
for (i = bb->start, insn = ctx->ir_base + i; i <= bb->end;) {
flags = ir_op_flags[insn->op];
@@ -682,8 +721,14 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f)
}
}
succ = ir_skip_empty_target_blocks(ctx, succ);
- if (succ != b + 1) {
- fprintf(f, "\t# GOTO BB%d\n", succ);
+ if (ctx->cfg_schedule) {
+ if (_b == ctx->cfg_blocks_count || succ != ctx->cfg_schedule[_b + 1]) {
+ fprintf(f, "\t# GOTO BB%d\n", succ);
+ }
+ } else {
+ if (succ != b + 1) {
+ fprintf(f, "\t# GOTO BB%d\n", succ);
+ }
}
} else if (insn->op == IR_IF) {
uint32_t true_block, false_block;
diff --git a/ext/opcache/jit/ir/ir_fold.h b/ext/opcache/jit/ir/ir_fold.h
index 9ff0f6d9db11..2e78990c7ce2 100644
--- a/ext/opcache/jit/ir/ir_fold.h
+++ b/ext/opcache/jit/ir/ir_fold.h
@@ -299,8 +299,12 @@ IR_FOLD(ADD(C_U64, C_U64))
}
IR_FOLD(ADD(C_ADDR, C_ADDR))
+IR_FOLD(ADD(C_ADDR, C_INTPTR))
+IR_FOLD(ADD(C_ADDR, C_UINTPTR))
+IR_FOLD(ADD(C_INTPTR, C_ADDR))
+IR_FOLD(ADD(C_UINTPTR, C_ADDR))
{
- IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
+// IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_U(op1_insn->val.addr + op2_insn->val.addr);
}
@@ -365,8 +369,12 @@ IR_FOLD(SUB(C_U64, C_U64))
}
IR_FOLD(SUB(C_ADDR, C_ADDR))
+IR_FOLD(SUB(C_ADDR, C_INTPTR))
+IR_FOLD(SUB(C_ADDR, C_UINTPTR))
+IR_FOLD(SUB(C_INTPTR, C_ADDR))
+IR_FOLD(SUB(C_UINTPTR, C_ADDR))
{
- IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
+// IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_U(op1_insn->val.addr - op2_insn->val.addr);
}
@@ -431,8 +439,12 @@ IR_FOLD(MUL(C_U64, C_U64))
}
IR_FOLD(MUL(C_ADDR, C_ADDR))
+IR_FOLD(MUL(C_ADDR, C_INTPTR))
+IR_FOLD(MUL(C_ADDR, C_UINTPTR))
+IR_FOLD(MUL(C_INTPTR, C_ADDR))
+IR_FOLD(MUL(C_UINTPTR, C_ADDR))
{
- IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
+// IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_U(op1_insn->val.addr * op2_insn->val.addr);
}
diff --git a/ext/opcache/jit/ir/ir_save.c b/ext/opcache/jit/ir/ir_save.c
index ab0bfb89b4db..3e5d7559e9c2 100644
--- a/ext/opcache/jit/ir/ir_save.c
+++ b/ext/opcache/jit/ir/ir_save.c
@@ -38,7 +38,55 @@ void ir_print_proto(const ir_ctx *ctx, ir_ref func_proto, FILE *f)
}
}
-void ir_save(const ir_ctx *ctx, FILE *f)
+static void ir_save_dessa_moves(const ir_ctx *ctx, int b, ir_block *bb, FILE *f)
+{
+ uint32_t succ;
+ ir_block *succ_bb;
+ ir_use_list *use_list;
+ ir_ref k, i, *p, use_ref, input;
+ ir_insn *use_insn;
+
+ IR_ASSERT(bb->successors_count == 1);
+ succ = ctx->cfg_edges[bb->successors];
+ succ_bb = &ctx->cfg_blocks[succ];
+ IR_ASSERT(succ_bb->predecessors_count > 1);
+ use_list = &ctx->use_lists[succ_bb->start];
+ k = ir_phi_input_number(ctx, succ_bb, b);
+
+ for (i = 0, p = &ctx->use_edges[use_list->refs]; i < use_list->count; i++, p++) {
+ use_ref = *p;
+ use_insn = &ctx->ir_base[use_ref];
+ if (use_insn->op == IR_PHI) {
+ input = ir_insn_op(use_insn, k);
+ if (IR_IS_CONST_REF(input)) {
+ fprintf(f, "\t# DESSA MOV c_%d", -input);
+ } else if (ctx->vregs[input] != ctx->vregs[use_ref]) {
+ fprintf(f, "\t# DESSA MOV d_%d {R%d}", input, ctx->vregs[input]);
+ } else {
+ continue;
+ }
+ if (ctx->regs) {
+ int8_t *regs = ctx->regs[use_ref];
+ int8_t reg = regs[k];
+ if (reg != IR_REG_NONE) {
+ fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[input].type),
+ (reg & (IR_REG_SPILL_LOAD|IR_REG_SPILL_SPECIAL)) ? ":load" : "");
+ }
+ }
+ fprintf(f, " -> d_%d {R%d}", use_ref, ctx->vregs[use_ref]);
+ if (ctx->regs) {
+ int8_t reg = ctx->regs[use_ref][0];
+ if (reg != IR_REG_NONE) {
+ fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[use_ref].type),
+ (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : "");
+ }
+ }
+ fprintf(f, "\n");
+ }
+ }
+}
+
+void ir_save(const ir_ctx *ctx, uint32_t save_flags, FILE *f)
{
ir_ref i, j, n, ref, *p;
ir_insn *insn;
@@ -65,16 +113,90 @@ void ir_save(const ir_ctx *ctx, FILE *f)
for (i = IR_UNUSED + 1, insn = ctx->ir_base + i; i < ctx->insns_count;) {
flags = ir_op_flags[insn->op];
+
+ if ((save_flags & IR_SAVE_CFG)
+ && ctx->cfg_map
+ && ctx->cfg_map[i]
+ && ctx->cfg_blocks[ctx->cfg_map[i]].start == i) {
+ uint32_t b = ctx->cfg_map[i];
+ ir_block *bb = &ctx->cfg_blocks[b];
+ fprintf(f, "#BB%d: end=l_%d", b, bb->end);
+ if (bb->flags && IR_BB_UNREACHABLE) {
+ fprintf(f, ", U");
+ }
+ if (bb->dom_parent > 0) {
+ fprintf(f, ", idom=BB%d(%d)", bb->dom_parent, bb->dom_depth);
+ }
+ if (bb->loop_depth != 0) {
+ if (bb->flags & IR_BB_LOOP_HEADER) {
+ if (bb->loop_header > 0) {
+ fprintf(f, ", loop=HDR,BB%d(%d)", bb->loop_header, bb->loop_depth);
+ } else {
+ IR_ASSERT(bb->loop_depth == 1);
+ fprintf(f, ", loop=HDR(%d)", bb->loop_depth);
+ }
+ } else {
+ IR_ASSERT(bb->loop_header > 0);
+ fprintf(f, ", loop=BB%d(%d)", bb->loop_header, bb->loop_depth);
+ }
+ }
+ if (bb->predecessors_count) {
+ uint32_t i;
+
+ fprintf(f, ", pred(%d)=[BB%d", bb->predecessors_count,
ctx->cfg_edges[bb->predecessors]);
+ for (i = 1; i < bb->predecessors_count; i++) {
+ fprintf(f, ", BB%d", ctx->cfg_edges[bb->predecessors + i]);
+ }
+ fprintf(f, "]");
+ }
+ if (bb->successors_count) {
+ uint32_t i;
+
+ fprintf(f, ", succ(%d)=[BB%d", bb->successors_count,
ctx->cfg_edges[bb->successors]);
+ for (i = 1; i < bb->successors_count; i++) {
+ fprintf(f, ", BB%d", ctx->cfg_edges[bb->successors + i]);
+ }
+ fprintf(f, "]");
+ }
+ fprintf(f, "\n");
+ }
+
if (flags & IR_OP_FLAG_CONTROL) {
if (!(flags & IR_OP_FLAG_MEM) || insn->type == IR_VOID) {
fprintf(f, "\tl_%d = ", i);
} else {
- fprintf(f, "\t%s d_%d, l_%d = ", ir_type_cname[insn->type], i, i);
+ fprintf(f, "\t%s d_%d", ir_type_cname[insn->type], i);
+ if (save_flags & IR_SAVE_REGS) {
+ if (ctx->vregs && ctx->vregs[i]) {
+ fprintf(f, " {R%d}", ctx->vregs[i]);
+ }
+ if (ctx->regs) {
+ int8_t reg = ctx->regs[i][0];
+ if (reg != IR_REG_NONE) {
+ fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), insn->type),
+ (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : "");
+ }
+ }
+ }
+ fprintf(f, ", l_%d = ", i);
}
} else {
fprintf(f, "\t");
if (flags & IR_OP_FLAG_DATA) {
- fprintf(f, "%s d_%d = ", ir_type_cname[insn->type], i);
+ fprintf(f, "%s d_%d", ir_type_cname[insn->type], i);
+ if (save_flags & IR_SAVE_REGS) {
+ if (ctx->vregs && ctx->vregs[i]) {
+ fprintf(f, " {R%d}", ctx->vregs[i]);
+ }
+ if (ctx->regs) {
+ int8_t reg = ctx->regs[i][0];
+ if (reg != IR_REG_NONE) {
+ fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), insn->type),
+ (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : "");
+ }
+ }
+ }
+ fprintf(f, " = ");
}
}
fprintf(f, "%s", ir_op_name[insn->op]);
@@ -101,6 +223,19 @@ void ir_save(const ir_ctx *ctx, FILE *f)
} else {
fprintf(f, "%sd_%d", first ? "(" : ", ", ref);
}
+ if (save_flags & IR_SAVE_REGS) {
+ if (ctx->vregs && ref > 0 && ctx->vregs[ref]) {
+ fprintf(f, " {R%d}", ctx->vregs[ref]);
+ }
+ if (ctx->regs) {
+ int8_t *regs = ctx->regs[i];
+ int8_t reg = regs[j];
+ if (reg != IR_REG_NONE) {
+ fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[ref].type),
+ (reg & (IR_REG_SPILL_LOAD|IR_REG_SPILL_SPECIAL)) ? ":load" : "");
+ }
+ }
+ }
first = 0;
break;
case IR_OPND_CONTROL:
@@ -141,14 +276,83 @@ void ir_save(const ir_ctx *ctx, FILE *f)
} else {
fprintf(f, ");");
}
+ first = 1;
if (((flags & IR_OP_FLAG_DATA) || ((flags & IR_OP_FLAG_MEM) && insn->type !=
IR_VOID)) && ctx->binding) {
ir_ref var = ir_binding_find(ctx, i);
if (var) {
IR_ASSERT(var < 0);
fprintf(f, " # BIND(0x%x);", -var);
+ first = 0;
+ }
+ }
+
+ if ((save_flags & IR_SAVE_CFG_MAP)
+ && ctx->cfg_map
+ && ctx->cfg_map[i]) {
+ if (first) {
+ fprintf(f, " #");
+ first = 0;
}
+ fprintf(f, " BLOCK=BB%d;", ctx->cfg_map[i]);
+ }
+
+ if ((save_flags & IR_SAVE_RULES)
+ && ctx->rules) {
+ uint32_t rule = ctx->rules[i];
+ uint32_t id = rule & IR_RULE_MASK;
+
+ if (first) {
+ fprintf(f, " #");
+ first = 0;
+ }
+ if (id < IR_LAST_OP) {
+ fprintf(f, " RULE(%s", ir_op_name[id]);
+ } else {
+ IR_ASSERT(id > IR_LAST_OP /*&& id < IR_LAST_RULE*/);
+ fprintf(f, " RULE(%s", ir_rule_name[id - IR_LAST_OP]);
+ }
+ if (rule & IR_FUSED) {
+ fprintf(f, ":FUSED");
+ }
+ if (rule & IR_SKIPPED) {
+ fprintf(f, ":SKIPPED");
+ }
+ if (rule & IR_SIMPLE) {
+ fprintf(f, ":SIMPLE");
+ }
+ fprintf(f, ");");
+ }
+
+ if ((save_flags & IR_SAVE_USE_LISTS)
+ && ctx->use_lists
+ && ctx->use_lists[i].count) {
+ ir_use_list *use_list = &ctx->use_lists[i];
+ ir_ref n = use_list->count;
+ ir_ref *p = ctx->use_edges + use_list->refs;
+
+ if (first) {
+ fprintf(f, " #");
+ first = 0;
+ }
+ fprintf(f, " USE_LIST(%d)=[%05d", n, *p);
+ for (p++, n--; n; p++, n--) {
+ fprintf(f, ", %05d", *p);
+ }
+ fprintf(f, "];");
}
fprintf(f, "\n");
+
+ if ((save_flags & (IR_SAVE_CFG|IR_SAVE_REGS)) == (IR_SAVE_CFG|IR_SAVE_REGS)
+ && ctx->cfg_map
+ && ctx->cfg_map[i]
+ && ctx->cfg_blocks[ctx->cfg_map[i]].end == i) {
+ uint32_t b = ctx->cfg_map[i];
+ ir_block *bb = &ctx->cfg_blocks[b];
+ if (bb->flags & IR_BB_DESSA_MOVES) {
+ ir_save_dessa_moves(ctx, b, bb, f);
+ }
+ }
+
n = ir_insn_inputs_to_len(n);
i += n;
insn += n;
diff --git a/ext/opcache/jit/ir/ir_x86.dasc b/ext/opcache/jit/ir/ir_x86.dasc
index 2c8507fec062..e0a406b21c23 100644
--- a/ext/opcache/jit/ir/ir_x86.dasc
+++ b/ext/opcache/jit/ir/ir_x86.dasc
@@ -2370,10 +2370,11 @@ store_int:
ctx->rules[op2_insn->op1] = IR_FUSED | IR_TEST_INT;
ctx->rules[insn->op2] = IR_FUSED | IR_SIMPLE | IR_NOP;
return IR_TEST_AND_BRANCH_INT;
- } else if ((op1_insn->op == IR_OR || op1_insn->op == IR_AND || op1_insn->op ==
IR_XOR) ||
- /* GT(ADD(_, _), 0) can't be optimized because ADD may overflow */
- ((op1_insn->op == IR_ADD || op1_insn->op == IR_SUB) &&
- (op2_insn->op == IR_EQ || op2_insn->op == IR_NE))) {
+ } else if (insn->op2 == ref - 1 && /* previous instruction */
+ ((op1_insn->op == IR_OR || op1_insn->op == IR_AND || op1_insn->op == IR_XOR) ||
+ /* GT(ADD(_, _), 0) can't be optimized because ADD may overflow */
+ ((op1_insn->op == IR_ADD || op1_insn->op == IR_SUB) &&
+ (op2_insn->op == IR_EQ || op2_insn->op == IR_NE)))) {
/* v = BINOP(_, _); c = CMP(v, 0) ... IF(c) => BINOP; SKIP_CMP ... JCC */
if (ir_op_flags[op1_insn->op] & IR_OP_FLAG_COMMUTATIVE) {
ir_match_fuse_load_commutative_int(ctx, op1_insn, ref);