Skip to content

Commit 9b6db61

Browse files
committed
factor out an 'optimize' phase for the X64 backend
1 parent b1ca52d commit 9b6db61

6 files changed

Lines changed: 23 additions & 4 deletions

File tree

BE/CodeGenX64/Makefile_cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ $(DIR)/isel_test_c: isel_gen.h isel_gen.cc
8484

8585
TEST_MODE = normal
8686
#TEST_MODE = legalize
87+
#TEST_MODE = optimize
8788
#TEST_MODE = reg_alloc_global
8889
#TEST_MODE = reg_alloc_local
8990
#TEST_MODE = binary

BE/CodeGenX64/codegen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def EmitUnitAsBinary(unit: ir.Unit) -> elf_unit.Unit:
247247
import sys
248248
import argparse
249249

250-
_ALLOWED_MODES = {"normal", "binary", "legalize", "reg_alloc_global",
250+
_ALLOWED_MODES = {"normal", "binary", "optimize", "legalize", "reg_alloc_global",
251251
"reg_alloc_local"}
252252

253253
def main():
@@ -284,6 +284,11 @@ def main():
284284

285285
fout = sys.stdout if args.output == "-" else open(args.output, "w")
286286

287+
legalize.OptimizeAll(unit, opt_stats)
288+
if args.mode == "optimize":
289+
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
290+
return
291+
287292
# we need to legalize all functions first as this may change the signature
288293
# and fills in cpu reg usage which is used by subsequent interprocedural opts.
289294
legalize.LegalizeAll(unit, opt_stats)

BE/CodeGenX64/codegen_tool.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ int main(int argc, const char* argv[]) {
168168
return 0;
169169
}
170170

171+
OptimizeAll(unit, false, log);
172+
if (sw_mode.Value() == "optimize") {
173+
UnitRenderToAsm(unit, fout);
174+
return 0;
175+
}
176+
171177
LegalizeAll(unit, false, log);
172178
if (sw_mode.Value() == "legalize") {
173179
UnitRenderToAsm(unit, fout);

BE/CodeGenX64/legalize.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ void PhaseFinalizeStackAndLocalRegAlloc(Fun fun, Unit unit,
532532
FunMoveEliminationCpu(fun, &inss);
533533
}
534534

535-
void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
535+
void OptimizeAll(Unit unit, bool verbose, std::ostream* fout) {
536536
std::vector<Fun> seeds;
537537
Fun fun = UnitFunFind(unit, StrNew("main"));
538538
if (!fun.isnull()) seeds.push_back(fun);
@@ -545,7 +545,11 @@ void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
545545
FunCfgInit(fun);
546546
FunOptBasic(fun, true);
547547
}
548+
}
549+
}
548550

551+
void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
552+
for (Fun fun : UnitFunIter(unit)) {
549553
FunCheck(fun);
550554
PhaseLegalization(fun, unit, fout);
551555
}

BE/CodeGenX64/legalize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern void PhaseFinalizeStackAndLocalRegAlloc(base::Fun fun,
2121
base::Unit unit,
2222
std::ostream* fout);
2323

24+
void OptimizeAll(base::Unit unit, bool verbose, std::ostream* fout);
25+
2426
void LegalizeAll(base::Unit unit, bool verbose, std::ostream* fout);
2527

2628
void RegAllocGlobal(base::Unit unit, bool verbose, std::ostream* fout);

BE/CodeGenX64/legalize.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def PhaseLegalization(fun: ir.Fun, unit: ir.Unit, _opt_stats: Dict[str, int]):
267267
# optimize.FunOptBasic(fun, opt_stats, allow_conv_conversion=False)
268268

269269

270-
def LegalizeAll(unit, opt_stats):
270+
def OptimizeAll(unit, opt_stats):
271271
seeds = [f for f in [unit.fun_syms.get("_start"),
272272
unit.fun_syms.get("main")] if f]
273273
if seeds:
@@ -279,8 +279,9 @@ def LegalizeAll(unit, opt_stats):
279279
optimize.FunCfgInit(fun, unit)
280280
optimize.FunOptBasic(fun, opt_stats, allow_conv_conversion=True)
281281

282+
283+
def LegalizeAll(unit, opt_stats):
282284
for fun in unit.funs:
283-
# pass
284285
PhaseLegalization(fun, unit, opt_stats)
285286

286287

0 commit comments

Comments
 (0)