Skip to content

Commit

Permalink
it fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RealNeGate committed Aug 8, 2024
1 parent 00ab67c commit 9bea2ef
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ bool tb_arena_free(TB_Arena* restrict arena, void* ptr, size_t size) {
void tb_arena_realign(TB_Arena* restrict arena) {
TB_ArenaChunk* top = arena->top;
ptrdiff_t pos = top->avail - top->data;
assert(pos >= 0);
pos = (pos + TB_ARENA_ALIGNMENT - 1) & ~(TB_ARENA_ALIGNMENT - 1);
top->avail = &top->data[pos];
}
Expand Down
4 changes: 2 additions & 2 deletions tb/src/codegen_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static void compile_function(TB_Function* restrict f, TB_FunctionOutput* restric
.num_classes = REG_CLASS_COUNT,
.emit = {
.output = func_out,
.arena = &f->tmp_arena,
.arena = code_arena,
.has_comments = true,
}
};
Expand Down Expand Up @@ -534,7 +534,7 @@ static void compile_function(TB_Function* restrict f, TB_FunctionOutput* restric
}

// trim code arena (it fits in a single chunk so just arena free the top)
code_arena->top->avail = (char*) &ctx.emit.data[ctx.emit.count];
tb_arena_free(code_arena, ctx.emit.data + ctx.emit.count, ctx.emit.capacity - ctx.emit.count);
tb_arena_realign(code_arena);

// TODO(NeGate): move the assembly output to code arena
Expand Down
12 changes: 8 additions & 4 deletions tb/src/opt/loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ static bool loop_inside(TB_LoopTree* a, TB_LoopTree* b) {
}

// returns the cloned loop header (with no preds)
static ArenaArray(TB_Node*) loop_clone_ztc(LoopOpt* ctx, TB_Worklist* ws, size_t cloned_n, TB_Node** cloned, TB_Node* header) {
static ArenaArray(TB_Node*) loop_clone_ztc(LoopOpt* ctx, TB_Worklist* ws, size_t cloned_n, TB_Node** cloned, TB_Node* header, TB_Node* latch) {
TB_Function* f = ctx->f;
ArenaArray(TB_Node*) cloned_list = aarray_create(&f->tmp_arena, TB_Node*, 32);

Expand Down Expand Up @@ -876,7 +876,7 @@ bool tb_opt_loops(TB_Function* f) {
TB_Node* ztc_start = header->inputs[0];
// construct the ZTC's version of the branch (same as the original latch but
// uses the phi's inputs[1] edge instead of the phis directly)
ArenaArray(TB_Node*) cloned_list = loop_clone_ztc(&ctx, &tmp_ws, cloned_n, cloned, header);
ArenaArray(TB_Node*) cloned_list = loop_clone_ztc(&ctx, &tmp_ws, cloned_n, cloned, header, latch);
TB_Node* top_cloned = cloned[header->gvn];
TB_Node* bot_cloned = cloned[latch->gvn];
// make a ZTC branch
Expand Down Expand Up @@ -933,7 +933,7 @@ bool tb_opt_loops(TB_Function* f) {
}

// any nodes created during this loop close fixup shouldn't themselves need fixup btw.
bool is_loop_phi = n->type == TB_PHI && n->inputs[0];
bool is_loop_phi = n->type == TB_PHI && n->inputs[0] == header;
size_t snapshot_count = f->node_count;

// lazily constructed
Expand Down Expand Up @@ -972,7 +972,11 @@ bool tb_opt_loops(TB_Function* f) {
p = phis[flavor] = tb_alloc_node(f, TB_PHI, n->dt, 3, 0);
set_input(f, p, r, 0);
set_input(f, p, init_path, 1);
set_input(f, p, n, 2);
if (is_loop_phi) {
set_input(f, p, n->inputs[2], 2);
} else {
set_input(f, p, n, 2);
}
loop_set_ctrl(&ctx, p, r);
mark_node(f, p);

Expand Down
8 changes: 5 additions & 3 deletions tb/src/opt/print_dumb.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ void tb_print_dumb_node(Lattice** types, TB_Node* n) {
}
printf(" = %s ", tb_node_get_name(n->type));
if (is_proj(n)) {
printf("%d ", TB_NODE_GET_EXTRA_T(n, TB_NodeProj)->index);
} else if (n->type == TB_MACH_PROJ) {
printf("%d ", TB_NODE_GET_EXTRA_T(n, TB_NodeMachProj)->index);
int idx = TB_NODE_GET_EXTRA_T(n, TB_NodeProj)->index;
printf("%d ", idx);
if (n->type == TB_BRANCH_PROJ && idx > 0) {
printf(", key=%lld ", TB_NODE_GET_EXTRA_T(n, TB_NodeBranchProj)->key);
}
} else if (n->type == TB_MACH_COPY) {
TB_NodeMachCopy* cpy = TB_NODE_GET_EXTRA(n);
printf("def=");
Expand Down
2 changes: 1 addition & 1 deletion tb/src/tb_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ for (uint64_t _bits_ = (bits), it = (start); _bits_; _bits_ >>= 1, ++it) if (_bi
#define TB_OPTDEBUG_PASSES 0
#define TB_OPTDEBUG_PEEP 0
#define TB_OPTDEBUG_SCCP 0
#define TB_OPTDEBUG_LOOP 1
#define TB_OPTDEBUG_LOOP 0
#define TB_OPTDEBUG_SROA 0
#define TB_OPTDEBUG_GCM 0
#define TB_OPTDEBUG_MEM2REG 0
Expand Down
58 changes: 39 additions & 19 deletions tb/src/x64/x64_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ static void print_extra(TB_Node* n) {
printf("scale=%d, disp=%d, mode=%s, imm=%d", 1u<<op->scale, op->disp, modes[op->mode], op->imm);
break;
}

case x86_cmpjcc: case x86_cmpimmjcc:
case x86_testjcc: case x86_testimmjcc:
case x86_ucomijcc:
{
X86MemOp* op = TB_NODE_GET_EXTRA(n);
printf("scale=%d, disp=%d, mode=%s, imm=%d", 1u<<op->scale, op->disp, modes[op->mode], op->imm);
break;
}
}
}

Expand Down Expand Up @@ -808,7 +817,20 @@ static TB_Node* node_isel(Ctx* restrict ctx, TB_Function* f, TB_Node* n) {
TB_Node* b = cond->inputs[2];

int cc = -1;
int flip = (if_br->key != 0);
switch (cond->type) {
case TB_CMP_EQ: cc = E; break;
case TB_CMP_NE: cc = NE; break;
case TB_CMP_SLT: cc = L; break;
case TB_CMP_SLE: cc = LE; break;
case TB_CMP_ULT: cc = B; break;
case TB_CMP_ULE: cc = BE; break;
case TB_CMP_FLT: cc = B; break;
case TB_CMP_FLE: cc = BE; break;
default: tb_unreachable();
}

// proj0 is the true case given key=0
int flip = (if_br->key == 0);
X86MemOp* op_extra;
if (TB_IS_FLOAT_TYPE(cmp_dt)) {
mach_cond = tb_alloc_node(f, x86_ucomijcc, TB_TYPE_TUPLE, 5, sizeof(X86MemOp));
Expand All @@ -817,7 +839,18 @@ static TB_Node* node_isel(Ctx* restrict ctx, TB_Function* f, TB_Node* n) {
set_input(f, mach_cond, b, 2);
} else {
if (a->type == TB_ICONST && b->type != TB_ICONST) {
flip ^= 1;
// swap operands so the compares get swapped (not inverted)
switch (cond->type) {
case TB_CMP_EQ: cc = E; break;
case TB_CMP_NE: cc = NE; break;
case TB_CMP_SLT: cc = G; break;
case TB_CMP_SLE: cc = GE; break;
case TB_CMP_ULT: cc = A; break;
case TB_CMP_ULE: cc = NB; break;
case TB_CMP_FLT: cc = A; break;
case TB_CMP_FLE: cc = NB; break;
default: tb_unreachable();
}
SWAP(TB_Node*, a, b);
}

Expand All @@ -839,19 +872,6 @@ static TB_Node* node_isel(Ctx* restrict ctx, TB_Function* f, TB_Node* n) {
set_input(f, mach_cond, a, 2);
}

if (cc < 0) {
switch (cond->type) {
case TB_CMP_EQ: cc = E; break;
case TB_CMP_NE: cc = NE; break;
case TB_CMP_SLT: cc = L; break;
case TB_CMP_SLE: cc = LE; break;
case TB_CMP_ULT: cc = B; break;
case TB_CMP_ULE: cc = BE; break;
case TB_CMP_FLT: cc = B; break;
case TB_CMP_FLE: cc = BE; break;
default: tb_unreachable();
}
}
op_extra->dt = cmp_dt;
if_br->key = cc ^ flip;
} else if (if_br->key == 0) {
Expand Down Expand Up @@ -2054,15 +2074,15 @@ static void bundle_emit(Ctx* restrict ctx, TB_CGEmitter* e, Bundle* bundle) {
TB_NodeBranchProj* if_br = cfg_if_branch(n);
if (if_br) {
Cond cc = if_br->key;
if (ctx->fallthrough == succ[0]) {
if (ctx->fallthrough == succ[1]) {
// if flipping avoids a jmp, do that
cc ^= 1;
SWAP(int, succ[0], succ[1]);
}

__(JO+cc, TB_X86_QWORD, Vlbl(succ[0]));
if (ctx->fallthrough != succ[1]) {
__(JMP, TB_X86_QWORD, Vlbl(succ[1]));
__(JO+cc, TB_X86_QWORD, Vlbl(succ[1]));
if (ctx->fallthrough != succ[0]) {
__(JMP, TB_X86_QWORD, Vlbl(succ[0]));
}
} else {
tb_todo();
Expand Down

0 comments on commit 9bea2ef

Please sign in to comment.