From f403e250bf4474cead6a6cbe5e917488f39163e9 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 13 Dec 2025 14:25:13 +0100 Subject: Add -O optimization flag --- common.h | 4 ++++ ir/ir.c | 6 ++++-- main.c | 6 ++++++ test/15-reloc.c | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/common.h b/common.h index 81c24ad..53669f4 100644 --- a/common.h +++ b/common.h @@ -132,6 +132,10 @@ struct option { bool nocolor; bool pie, pic; bool werror; + enum optz { + OPT0 = -1, + OPT1 = 1, + } o; union { struct { bool p : 1, /* after parsing */ diff --git a/ir/ir.c b/ir/ir.c index cd93992..3befc18 100644 --- a/ir/ir.c +++ b/ir/ir.c @@ -605,8 +605,10 @@ irfini(struct function *fn) abi0(fn); lowerintrin(fn); - mem2reg(fn); - copyopt(fn); + if (ccopt.o > OPT0) { + mem2reg(fn); + copyopt(fn); + } if (ccopt.dbg.o) { bfmt(ccopt.dbgout, "<< Before isel >>\n"); irdump(fn); diff --git a/main.c b/main.c index 9cf55af..5fa90fd 100644 --- a/main.c +++ b/main.c @@ -185,6 +185,10 @@ optparse(char **args) } } else if (*arg == 'g') { /* TODO debug info */ + } else if (*arg == 'O') { + if (!arg[1] || (uint)arg[1] - '1' < 9) ccopt.o = OPT1; + else if (arg[1] == '0') ccopt.o = OPT0; + else goto Bad; } else if (*arg == 'D' || *arg == 'U') { void cpppredef(bool undef, const char *cmd); const char *def = arg[1] ? arg+1 : *++args; @@ -510,6 +514,8 @@ prihelp(void) " -llib \tLink with library\n" " -fpie \tEmit code for position independent executable\n" " -fpic \tEmit position independent code\n" + " -O0 \tTurn off optimizations\n" + " -O1 \tTurn on more optimizations\n" " -x[c|o] \tSpecify type of next input file (C, object)\n" ); } diff --git a/test/15-reloc.c b/test/15-reloc.c index 8d1b323..cd91fd0 100644 --- a/test/15-reloc.c +++ b/test/15-reloc.c @@ -1,3 +1,4 @@ +/* CFLAGS: -O0 */ /* EXPECT: 1.3 5 -- cgit v1.2.3