Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement constant optimization #116

Merged
merged 3 commits into from
Mar 4, 2024
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
5 changes: 3 additions & 2 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#define MAX_PARAMS 8
#define MAX_LOCALS 1450
#define MAX_FIELDS 32
#define MAX_FUNCS 256
#define MAX_FUNC_TRIES 1950
#define MAX_FUNCS 512
#define MAX_FUNC_TRIES 2160
#define MAX_BLOCKS 1150
#define MAX_TYPES 64
#define MAX_IR_INSTR 36864
Expand Down Expand Up @@ -175,6 +175,7 @@ struct var {
ref_block_list_t ref_block_list; /* blocks which kill variable */
int consumed;
int is_ternary_ret;
int is_const; /* whether a constant representaion or not */
};

typedef struct var var_t;
Expand Down
1 change: 1 addition & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var_t *require_var(block_t *blk)
error("Too many locals");

var_t *var = &blk->locals[blk->next_local++];
var->consumed = -1;
var->base = var;
return var;
}
Expand Down
15 changes: 15 additions & 0 deletions src/reg-alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,22 @@ void reg_alloc()
break;
case OP_load_constant:
case OP_load_data_address:
if (insn->rd->consumed == -1)
break;

dest = prepare_dest(bb, insn->rd, -1, -1);
ir = bb_add_ph2_ir(bb, insn->opcode);
ir->src0 = insn->rd->init_val;
ir->dest = dest;

/* store global variable immediately after assignment */
if (insn->rd->is_global) {
ir = bb_add_ph2_ir(bb, OP_global_store);
ir->src0 = dest;
ir->src1 = insn->rd->offset;
REGS[dest].polluted = 0;
}

break;
case OP_address_of:
/* make sure variable is on stack */
Expand All @@ -412,6 +424,9 @@ void reg_alloc()
ir->dest = dest;
break;
case OP_assign:
if (insn->rd->consumed == -1)
break;

src0 = find_in_regs(insn->rs1);

/* If operand is loaded from stack, clear the original slot
Expand Down
84 changes: 83 additions & 1 deletion src/ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,87 @@ int cse(insn_t *insn, basic_block_t *bb)
return 1;
}

int mark_const(insn_t *insn)
{
if (insn->opcode == OP_load_constant) {
insn->rd->is_const = 1;
return 0;
}
if (insn->opcode != OP_assign)
return 0;
/* The global variable is unique and has no subscripts in our SSA. Do NOT
* evaluate its value.
*/
if (insn->rd->is_global)
return 0;
if (!insn->rs1->is_const) {
if (!insn->prev)
return 0;
if (insn->prev->opcode != OP_load_constant)
return 0;
if (insn->rs1 != insn->prev->rd)
return 0;
}

insn->opcode = OP_load_constant;
insn->rd->is_const = 1;
insn->rd->init_val = insn->rs1->init_val;
insn->rs1 = NULL;
return 1;
}

int eval_const_arithmetic(insn_t *insn)
{
if (!insn->rs1)
return 0;
if (!insn->rs1->is_const)
return 0;
if (!insn->rs2)
return 0;
if (!insn->rs2->is_const)
return 0;

int res;
int l = insn->rs1->init_val;
int r = insn->rs2->init_val;

switch (insn->opcode) {
case OP_add:
res = l + r;
break;
case OP_sub:
res = l - r;
break;
case OP_mul:
res = l * r;
break;
case OP_div:
res = l / r;
break;
case OP_mod:
res = l % r;
break;
default:
return 0;
}

insn->rs1 = NULL;
insn->rs2 = NULL;
insn->rd->is_const = 1;
insn->rd->init_val = res;
insn->opcode = OP_load_constant;
return 1;
}

int const_folding(insn_t *insn)
{
if (mark_const(insn))
return 1;
if (eval_const_arithmetic(insn))
return 1;
return 0;
}

void optimize()
{
fn_t *fn;
Expand All @@ -1124,7 +1205,8 @@ void optimize()
for (insn = bb->insn_list.head; insn; insn = insn->next) {
if (cse(insn, bb))
continue;

if (const_folding(insn))
continue;
/* more optimizations */
}
}
Expand Down
16 changes: 15 additions & 1 deletion tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ int main()
}
EOF

# optimizer
# optimizers

# common subexpression elimination (CSE)
try_ 1 << EOF
int i = 0;
void func()
Expand All @@ -566,4 +568,16 @@ int main()
}
EOF

# constant folding
try_ 20 << EOF
int main()
{
int a = 2; /* constant assingment */
int b = a; /* assignment via constant representation */
int c = a + b;
int d = c + 8; /* mixed assigment */
return a + b + c + d; /* chained assignment */
}
EOF

echo OK
Loading