Skip to content

Commit

Permalink
Apply SSA by command line option
Browse files Browse the repository at this point in the history
Default: off.
  • Loading branch information
tyfkda committed Nov 24, 2024
1 parent 1e18411 commit 504bbf1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: test xcc gen2
run: make test-gen2

- name: test ssa
run: make -C tests clean && make XCC="../xcc --apply-ssa" -C tests cc-tests test-examples

- name: setup
run: |
npm ci
Expand Down
9 changes: 9 additions & 0 deletions src/_debug/dump_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ int main(int argc, char *argv[]) {
enum {
OPT_KEEP_VIRTUAL_REGISTER = 128,
OPT_KEEP_PHI,
OPT_SSA,
};

static const struct option options[] = {
{"-keep-virtual", no_argument, OPT_KEEP_VIRTUAL_REGISTER},
{"-keep-phi", no_argument, OPT_KEEP_PHI},
{"-apply-ssa", no_argument, OPT_SSA},

{NULL},
};
Expand All @@ -377,6 +379,13 @@ int main(int argc, char *argv[]) {
keep_phi = true;
break;

case OPT_SSA:
{
extern bool apply_ssa;
apply_ssa = true;
}
break;

case '?':
fprintf(stderr, "Warning: unknown option: %s\n", argv[optind - 1]);
break;
Expand Down
16 changes: 11 additions & 5 deletions src/cc/backend/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "util.h"

bool keep_phi;
bool apply_ssa;

static IR *is_last_jmp(BB *bb) {
int len;
Expand Down Expand Up @@ -542,11 +543,16 @@ void optimize(RegAlloc *ra, BBContainer *bbcon) {
peephole(ra, bb);
}

make_ssa(ra, bbcon);
copy_propagation(ra, bbcon);
remove_unused_vregs(ra, bbcon);
if (!keep_phi) {
resolve_phis(ra, bbcon);
if (apply_ssa) {
make_ssa(ra, bbcon);
copy_propagation(ra, bbcon);
remove_unused_vregs(ra, bbcon);
if (!keep_phi) {
resolve_phis(ra, bbcon);
remove_unnecessary_bb(bbcon);
}
} else {
remove_unused_vregs(ra, bbcon);
remove_unnecessary_bb(bbcon);
}
detect_from_bbs(bbcon);
Expand Down
12 changes: 12 additions & 0 deletions src/cc/cc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "emit_code.h"
#include "fe_misc.h"
#include "lexer.h"
#include "optimize.h" // opt_flags
#include "parser.h"
#include "type.h"
#include "util.h"
Expand Down Expand Up @@ -70,6 +71,7 @@ static bool parse_fopt(const char *optarg, bool value) {
int main(int argc, char *argv[]) {
enum {
OPT_FNO = 128,
OPT_SSA,
};

static const struct option options[] = {
Expand All @@ -82,6 +84,9 @@ int main(int argc, char *argv[]) {
{"f", required_argument},
{"W", required_argument},

// Feature flag.
{"-apply-ssa", no_argument, OPT_SSA},

{NULL},
};
int opt;
Expand Down Expand Up @@ -130,6 +135,13 @@ int main(int argc, char *argv[]) {
}
break;

case OPT_SSA:
{
extern bool apply_ssa;
apply_ssa = true;
}
break;

case '?':
fprintf(stderr, "Warning: unknown option: %s\n", argv[optind - 1]);
break;
Expand Down
9 changes: 9 additions & 0 deletions src/xcc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ static void parse_options(int argc, char *argv[], Options *opts) {
OPT_PEDANTIC,
OPT_MMD,
OPT_NO_PIE,

OPT_SSA,
};

static const struct option kOptions[] = {
Expand Down Expand Up @@ -340,6 +342,9 @@ static void parse_options(int argc, char *argv[], Options *opts) {
{"MMD", no_argument, OPT_MMD},
{"no-pie", no_argument, OPT_NO_PIE},

// Feature flag.
{"-apply-ssa", no_argument, OPT_SSA},

{NULL},
};

Expand Down Expand Up @@ -503,6 +508,10 @@ static void parse_options(int argc, char *argv[], Options *opts) {
// Silently ignored.
vec_push(opts->linker_options, argv[optind - 1]);
break;

case OPT_SSA:
vec_push(opts->cc1_cmd, argv[optind - 1]);
break;
}
}
}
Expand Down

0 comments on commit 504bbf1

Please sign in to comment.