Skip to content

Commit

Permalink
compiling again
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed May 29, 2024
1 parent f0358ae commit 4bf833b
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 164 deletions.
5 changes: 4 additions & 1 deletion ir/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ void vx_IrOp_remove_successor(vx_IrOp *op) {
if (op && op->next) {
vx_IrOp *newnext = op->next->next;
vx_IrOp *old = op->next;
old->next = NULL;

// WE DON'T DO THAT BECAUSE WE CALL REMOVE WHILE ITERATING IN THE PASSES
// old->next = NULL;

op->next = newnext;
}
}
Expand Down
8 changes: 3 additions & 5 deletions ir/opt/cmov.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ static vx_IrVar block_res_as_var(vx_IrBlock *parent, vx_IrBlock *block) {
// cost of then + else is small enough
// exactly one out

void vx_opt_cmov(vx_IrView view, vx_IrBlock *block)
void vx_opt_cmov(vx_IrBlock *block)
{
for (size_t i = view.start; i < view.end; i ++) {
vx_IrOp *op = &block->ops[i];

for (vx_IrOp *op = block->first; op; op = op->next) {
if (op->id != VX_IR_OP_IF)
continue;

Expand Down Expand Up @@ -59,7 +57,7 @@ void vx_opt_cmov(vx_IrView view, vx_IrBlock *block)
vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block;


vx_IrBlock *body = vx_IrBlock_init_heap(block, i);
vx_IrBlock *body = vx_IrBlock_init_heap(block, op);

vx_IrVar v0 = block_res_as_var(body, then);
vx_IrVar v1 = block_res_as_var(body, els);
Expand Down
7 changes: 2 additions & 5 deletions ir/opt/comparisions.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
* `constant < a` -> `a > constant`
* `constant > a` -> `a < constant`
*/
void vx_opt_comparisions(vx_IrView view,
vx_IrBlock *block)
void vx_opt_comparisions(vx_IrBlock *block)
{
for (size_t i = view.start; i < view.end; i ++) {
vx_IrOp *op = &block->ops[i];

for (vx_IrOp *op = block->first; op; op = op->next) {
switch (op->id) {
case VX_IR_OP_LTE: {
vx_IrValue *b = vx_IrOp_param(op, VX_IR_NAME_OPERAND_B);
Expand Down
12 changes: 2 additions & 10 deletions ir/opt/constant_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

#include <assert.h>

void vx_opt_constant_eval(vx_IrView view,
vx_IrBlock *block)
void vx_opt_constant_eval(vx_IrBlock *block)
{
assert(view.block == block);

while (vx_IrView_has_next(view)) {
// "unsafe", but we own the underlying block anyways
vx_IrOp *op = (vx_IrOp *) vx_IrView_take(view);

for (vx_IrOp *op = block->first; op; op = op->next) {
#define BINARY(what) { \
vx_IrValue *a = vx_IrOp_param(op, VX_IR_NAME_OPERAND_A); \
vx_IrValue *b = vx_IrOp_param(op, VX_IR_NAME_OPERAND_B); \
Expand Down Expand Up @@ -140,7 +134,5 @@ void vx_opt_constant_eval(vx_IrView view,
default:
break;
}

view = vx_IrView_drop(view, 1);
}
}
18 changes: 6 additions & 12 deletions ir/opt/dce.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
#include "../opt.h"

static void rmcode_before_label(vx_IrBlock *block, size_t first) {
for (size_t i = first; i < block->ops_len; i ++) {
vx_IrOp *op = &block->ops[i];

static void rmcode_before_label(vx_IrOp *op) {
for (; op; op = op->next) {
if (op->id == VX_LIR_OP_LABEL)
break;

vx_IrOp_destroy(op);
vx_IrOp_init(op, VX_IR_OP_NOP, block);
vx_IrOp_remove(op);
}
}

void vx_opt_ll_dce(vx_IrBlock *block) {
for (size_t i = 0; i < block->ops_len; i ++) {
vx_IrOp *op = &block->ops[i];
if (vx_IrOp_ends_flow(op)) {
rmcode_before_label(block, i + 1);
}
}
for (vx_IrOp *op = block->first; op; op = op->next)
if (vx_IrOp_ends_flow(op))
rmcode_before_label(op->next);
}

11 changes: 4 additions & 7 deletions ir/opt/inline_vars.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "../opt.h"

static bool trav (vx_IrOp *op,
void *data)
static bool trav (vx_IrOp *op, void *data)
{
vx_IrBlock *block = data;

Expand All @@ -12,14 +11,12 @@ static bool trav (vx_IrOp *op,

for (size_t i = 0; i < op->outs_len; i ++) {
const vx_IrVar out = op->outs[i].var;
vx_IrView_substitute_var(vx_IrView_of_all(block), block, out, value);
vx_IrBlock_substitute_var(block, out, value);
}

return false;
}

void vx_opt_inline_vars(vx_IrView view,
vx_IrBlock *block)
{
vx_IrView_deep_traverse(view, trav, block);
void vx_opt_inline_vars(vx_IrBlock *block) {
vx_IrBlock_deep_traverse(block, trav, block);
}
14 changes: 3 additions & 11 deletions ir/opt/join_compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
/**
* Find identical computations and join them
*/
void vx_opt_join_compute(vx_IrView view,
vx_IrBlock *block)
void vx_opt_join_compute(vx_IrBlock *block)
{
assert(view.block == block);

for (size_t i = 0; i < block->ops_len; i ++) {
vx_IrOp *op = &block->ops[i];

size_t j = i;
while (j --> 0) {
vx_IrOp *prev = &block->ops[j];

for (vx_IrOp *op = block->first; op; op = op->next) {
for (vx_IrOp *prev = block->first; prev != op; prev = prev->next) {
if (prev->id != op->id || vx_IrOp_is_volatile(op) || prev->params_len != op->params_len || prev->outs_len == 0)
continue;

Expand Down
33 changes: 15 additions & 18 deletions ir/opt/ll_jumps.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
// we go trough all ops between the jump and the label
// if all of them have no effect (labels, nops), we can remove the jump instruction
static void part1(vx_IrBlock *block) {
vx_IrBlock *root = (vx_IrBlock*)vx_IrBlock_root(block);
for (size_t opid = 0; opid < block->ops_len; opid ++) {
vx_IrOp *op = &block->ops[opid];
vx_IrBlock *root = vx_IrBlock_root(block);

for (vx_IrOp *op = block->first; op; op = op->next) {
if (!(op->id == VX_LIR_GOTO || op->id == VX_LIR_COND))
continue;

size_t label_id = vx_IrOp_param(op, VX_IR_NAME_ID)->id;

vx_IrOp *decl = vx_IrBlock_root_get_label_decl(root, label_id);
vx_IrOp *decl = root->as_root.labels[label_id].decl;

if (decl->parent != op->parent)
continue;
Expand All @@ -23,8 +22,11 @@ static void part1(vx_IrBlock *block) {
continue;

bool can_opt = true;
for (size_t i = (op - block->ops) + 1; i < (size_t) (decl - block->ops); i ++) {
if (vx_IrOpType_has_effect(block->ops[i].id)) {
for (vx_IrOp *other = op->next; other; other = other->next) {
if (other == decl)
break;

if (vx_IrOpType_has_effect(other->id)) {
can_opt = false;
break;
}
Expand All @@ -33,30 +35,26 @@ static void part1(vx_IrBlock *block) {
if (!can_opt)
continue;

vx_IrOp_destroy(op);

vx_IrOp_init(op, VX_IR_OP_NOP, block);
vx_IrOp_remove(op);
}
}

// is the jump dest just another jump? optimize that
static void part2(vx_IrBlock *block) {
vx_IrBlock *root = (vx_IrBlock*)vx_IrBlock_root(block);
for (size_t opid = 0; opid < block->ops_len; opid ++) {
vx_IrOp *op = &block->ops[opid];
vx_IrBlock *root = vx_IrBlock_root(block);

for (vx_IrOp *op = block->first; op; op = op->next) {
if (!(op->id == VX_LIR_GOTO || op->id == VX_LIR_COND))
continue;

size_t label_id = vx_IrOp_param(op, VX_IR_NAME_ID)->id;

vx_IrOp *decl = vx_IrBlock_root_get_label_decl(root, label_id);
vx_IrOp *decl = root->as_root.labels[label_id].decl;
if (decl->parent != block)
continue;
size_t decl_id = decl - block->ops;

if (decl_id + 1 < block->ops_len) {
vx_IrOp *following = &block->ops[decl_id + 1];
if (decl->next) {
vx_IrOp *following = decl->next;

if (following->id == VX_LIR_GOTO) {
label_id = vx_IrOp_param(following, VX_IR_NAME_ID)->id;
Expand All @@ -66,8 +64,7 @@ static void part2(vx_IrBlock *block) {
}
}

void vx_opt_ll_jumps(vx_IrView view, vx_IrBlock *block) {
assert(view.block == block);
void vx_opt_ll_jumps(vx_IrBlock *block) {
part1(block);
part2(block);
}
Expand Down
79 changes: 29 additions & 50 deletions ir/opt/loop_simplify.c
Original file line number Diff line number Diff line change
@@ -1,70 +1,49 @@
#include "../opt.h"

void vx_opt_loop_simplify(vx_IrView view,
vx_IrBlock *block)
void vx_opt_loop_simplify(vx_IrBlock *block)
{
while (vx_IrView_find(&view, VX_IR_OP_FOR)) {
vx_IrOp *op = (vx_IrOp *) vx_IrView_take(view);
for (vx_IrOp *op = block->first; op; op = op->next) {
if (op->id != VX_IR_OP_FOR)
continue;

vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block;
const vx_IrVar condVar = cond->outs[0];

// if it will always we 0, we optimize it out
if (vx_Irblock_alwaysis_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) {
vx_IrView_replace(block, view, NULL, 0);

goto next;
vx_IrOp_remove(op);
continue;
}

// if it will never be 0 (not might be 0), it is always true => infinite loop
if (!vx_Irblock_mightbe_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) {
vx_IrOp new;
vx_IrOp_init(&new, VX_IR_OP_INFINITE, block);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE);
vx_IrOp_steal_states(&new, op);
vx_IrOp_steal_outs(&new, op);

vx_IrView_replace(block, view, &new, 1);

goto next;
op->id = VX_IR_OP_INFINITE; // we could remove cond param
continue;
}

// if it is a less than, we can do a repeat
if (cond->ops_len > 0 && cond->ops[0].id == VX_IR_OP_LT) {
if (cond->first && cond->first->id == VX_IR_OP_LT) {
const vx_IrValue *a = vx_IrOp_param(cond->first, VX_IR_NAME_OPERAND_A);
// require it to be the counter
if (a->type != VX_IR_VAL_VAR)
continue;
if (a->var != cond->ins[0].var)
continue;
if (cond->first->outs[0].var != cond->outs[0])
continue;

const vx_IrValue b = *vx_IrOp_param(cond->first, VX_IR_NAME_OPERAND_B);

bool break2 = false;
do {
const vx_IrValue *a = vx_IrOp_param(&cond->ops[0], VX_IR_NAME_OPERAND_A);
// require it to be the counter
if (a->type != VX_IR_VAL_VAR)
break;
if (a->var != cond->ins[0].var)
break;
if (cond->ops[0].outs[0].var != cond->outs[0])
break;

const vx_IrValue b = *vx_IrOp_param(&cond->ops[0], VX_IR_NAME_OPERAND_B);

vx_IrOp new;
vx_IrOp_init(&new, VX_IR_OP_REPEAT, block);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START);
vx_IrOp_add_param_s(&new, VX_IR_NAME_LOOP_ENDEX, b);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE);
vx_IrOp_steal_states(&new, op); // steal all state init params
vx_IrOp_steal_outs(&new, op);
// we probably do a bit of memory leaking here...
*op = new;

break2 = true;
} while(0);
if (break2)
goto next; // we optimized already
vx_IrOp new;
vx_IrOp_init(&new, VX_IR_OP_REPEAT, block);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START);
vx_IrOp_add_param_s(&new, VX_IR_NAME_LOOP_ENDEX, b);
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE);
vx_IrOp_steal_states(&new, op); // steal all state init params
vx_IrOp_steal_outs(&new, op);
// we probably do a bit of memory leaking here...
*op = new;
}

next:
view = vx_IrView_drop(view, 1);
}
}
Loading

0 comments on commit 4bf833b

Please sign in to comment.