Skip to content

Commit 3aaa176

Browse files
committed
move common code to cfg and more unbreaking
1 parent 874d3e4 commit 3aaa176

6 files changed

Lines changed: 63 additions & 54 deletions

File tree

BE/Base/cfg.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,13 @@ void UnitRemoveUnreachableCode(Unit unit, const std::vector<Fun>& seeds) {
477477
}
478478
}
479479

480+
std::vector<Fun> UnitGetEntryPoints(Unit unit) {
481+
std::vector<Fun> seeds;
482+
Fun fun = UnitFunFind(unit, StrNew("main"));
483+
if (!fun.isnull()) seeds.push_back(fun);
484+
fun = UnitFunFind(unit, StrNew("_start"));
485+
if (!fun.isnull()) seeds.push_back(fun);
486+
return seeds;
487+
}
488+
480489
} // namespace cwerg::base

BE/Base/cfg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
// (c) Robert Muth - see LICENSE for more info
33

4+
#include <vector>
45
#include "BE/Base/ir.h"
56

67
namespace cwerg::base {
@@ -31,4 +32,6 @@ extern Str NewDerivedBblName(Str orig_name, const char* suffix, Fun fun);
3132

3233
extern void UnitRemoveUnreachableCode(Unit unit, const std::vector<Fun>& seeds);
3334

35+
extern std::vector<Fun> UnitGetEntryPoints(Unit unit);
36+
3437
} // namespace cwerg

BE/Base/cfg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,8 @@ def UnitRemoveUnreachableCode(unit: ir.Unit, seeds: List[ir.Fun]):
324324
ir.FUN_FLAG.REACHACHABLE not in op.flags):
325325
reachable.add(op)
326326
unit.funs = [f for f in unit.funs if ir.FUN_FLAG.REACHACHABLE in f.flags]
327+
328+
329+
def UnitGetEntryPoints(unit: ir.Unit) -> list[ir.Fun]:
330+
return [f for f in [unit.fun_syms.get("_start"),
331+
unit.fun_syms.get("main")] if f]

BE/CodeGenA64/codegen.py

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import stat
99
import collections
1010

11-
from BE.Base import cfg
1211
from BE.Base import ir
1312
from IR import opcode_tab as o
14-
from BE.Base import sanity
1513
from BE.Base import serialize
1614

1715
from BE.CpuA64 import opcode_tab as a64
@@ -26,41 +24,6 @@
2624
from BE.Elf import elf_unit
2725

2826

29-
def LegalizeAll(unit, opt_stats, fout, verbose=False):
30-
seeds = [f for f in [unit.fun_syms.get("_start"),
31-
unit.fun_syms.get("main")] if f]
32-
if seeds:
33-
cfg.UnitRemoveUnreachableCode(unit, seeds)
34-
for fun in unit.funs:
35-
sanity.FunCheck(fun, unit, check_cfg=False,
36-
check_push_pop=True, check_fallthroughs=False)
37-
38-
if fun.kind is o.FUN_KIND.NORMAL:
39-
legalize.PhaseOptimize(fun, unit, opt_stats, fout)
40-
41-
for fun in unit.funs:
42-
legalize.PhaseLegalizationStep1(fun, unit, opt_stats, fout)
43-
44-
for fun in unit.funs:
45-
legalize.PhaseLegalizationStep2(fun, unit, opt_stats, fout)
46-
47-
48-
def RegAllocGlobal(unit, opt_stats, fout, verbose=False):
49-
for fun in unit.funs:
50-
sanity.FunCheck(fun, unit, check_cfg=False,
51-
check_push_pop=False, check_fallthroughs=False)
52-
legalize.PhaseGlobalRegAlloc(fun, opt_stats, fout)
53-
if verbose:
54-
legalize.DumpFun("after global_reg_alloc", fun)
55-
56-
57-
def RegAllocLocal(unit, opt_stats, fout, verbose=False):
58-
for fun in unit.funs:
59-
legalize.PhaseFinalizeStackAndLocalRegAlloc(fun, opt_stats, fout)
60-
if verbose:
61-
legalize.DumpFun("after stack finalization", fun)
62-
63-
6427
############################################################
6528
# textual emitter
6629
############################################################
@@ -294,9 +257,9 @@ def main():
294257
if args.mode == "binary":
295258
# we need to legalize all functions first as this may change the signature
296259
# and fills in cpu reg usage which is used by subsequent interprocedural opts.
297-
LegalizeAll(unit, opt_stats, None)
298-
RegAllocGlobal(unit, opt_stats, None)
299-
RegAllocLocal(unit, opt_stats, None)
260+
legalize.LegalizeAll(unit, opt_stats, None)
261+
legalize.RegAllocGlobal(unit, opt_stats, None)
262+
legalize.RegAllocLocal(unit, opt_stats, None)
300263
armunit = EmitUnitAsBinary(unit)
301264
exe = assembler.Assemble(armunit, True)
302265
exe.save(open(args.output, "wb"))
@@ -307,17 +270,17 @@ def main():
307270

308271
# we need to legalize all functions first as this may change the signature
309272
# and fills in cpu reg usage which is used by subsequent interprocedural opts.
310-
LegalizeAll(unit, opt_stats, fout)
273+
legalize.LegalizeAll(unit, opt_stats, fout)
311274
if args.mode == "legalize":
312275
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
313276
return
314277

315-
RegAllocGlobal(unit, opt_stats, fout)
278+
legalize.RegAllocGlobal(unit, opt_stats, fout)
316279
if args.mode == "reg_alloc_global":
317280
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
318281
return
319282

320-
RegAllocLocal(unit, opt_stats, fout)
283+
legalize.RegAllocLocal(unit, opt_stats, fout)
321284
if args.mode == "reg_alloc_local":
322285
print("\n".join(serialize.UnitRenderToASM(unit)), file=fout)
323286
return

BE/CodeGenA64/legalize.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,25 +363,18 @@ void PhaseFinalizeStackAndLocalRegAlloc(Fun fun, Unit unit,
363363
FunMoveEliminationCpu(fun, &inss);
364364
}
365365

366-
std::vector<Fun> GetSeeds(Unit unit) {
367-
std::vector<Fun> seeds;
368-
Fun fun = UnitFunFind(unit, StrNew("main"));
369-
if (!fun.isnull()) seeds.push_back(fun);
370-
fun = UnitFunFind(unit, StrNew("_start"));
371-
if (!fun.isnull()) seeds.push_back(fun);
372-
return seeds;
373-
}
374-
375366
void LegalizeAll(Unit unit, bool verbose, std::ostream* fout) {
376-
std::vector<Fun> seeds = GetSeeds(unit);
367+
std::vector<Fun> seeds = UnitGetEntryPoints(unit);
377368
if (!seeds.empty()) UnitRemoveUnreachableCode(unit, seeds);
378369
for (Fun fun : UnitFunIter(unit)) {
379370
FunCheck(fun, false, true, false);
380371
if (FunKind(fun) == FUN_KIND::NORMAL) {
381372
FunCfgInit(fun);
382373
FunOptBasic(fun, true);
383374
}
375+
}
384376

377+
for (Fun fun : UnitFunIter(unit)) {
385378
PhaseLegalizationStep1(fun, unit, fout);
386379
PhaseLegalizationStep2(fun, unit, fout);
387380
}

BE/CodeGenA64/legalize.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import dataclasses
33
from typing import List, Dict, Optional, Tuple
44

5+
from BE.Base import cfg
56
from BE.Base import canonicalize
67
from BE.Base import reg_alloc
78
from BE.Base import ir
@@ -275,7 +276,8 @@ def PhaseLegalizationStep2(fun: ir.Fun, unit: ir.Unit, _opt_stats: Dict[str, int
275276
# This excludes immediates related to stack offsets which have not been determined yet
276277
_FunRewriteOutOfBoundsImmediates(fun, unit)
277278

278-
sanity.FunCheck(fun, None, check_cfg=True, check_push_pop=False, check_fallthroughs=False)
279+
sanity.FunCheck(fun, None, check_cfg=True,
280+
check_push_pop=False, check_fallthroughs=False)
279281
# optimize.FunOptBasic(fun, opt_stats, allow_conv_conversion=False)
280282

281283

@@ -404,3 +406,37 @@ def PhaseFinalizeStackAndLocalRegAlloc(fun: ir.Fun,
404406
# cleanup
405407
_FunMoveEliminationCpu(fun)
406408
# print ("@@@@@@\n", "\n".join(serialize.FunRenderToAsm(fun)))
409+
410+
411+
def LegalizeAll(unit, opt_stats, fout, verbose=False):
412+
seeds = cfg.UnitGetEntryPoints(unit)
413+
if seeds:
414+
cfg.UnitRemoveUnreachableCode(unit, seeds)
415+
for fun in unit.funs:
416+
sanity.FunCheck(fun, unit, check_cfg=False,
417+
check_push_pop=True, check_fallthroughs=False)
418+
419+
if fun.kind is o.FUN_KIND.NORMAL:
420+
PhaseOptimize(fun, unit, opt_stats, fout)
421+
422+
for fun in unit.funs:
423+
PhaseLegalizationStep1(fun, unit, opt_stats, fout)
424+
425+
for fun in unit.funs:
426+
PhaseLegalizationStep2(fun, unit, opt_stats, fout)
427+
428+
429+
def RegAllocGlobal(unit, opt_stats, fout, verbose=False):
430+
for fun in unit.funs:
431+
sanity.FunCheck(fun, unit, check_cfg=False,
432+
check_push_pop=False, check_fallthroughs=False)
433+
PhaseGlobalRegAlloc(fun, opt_stats, fout)
434+
if verbose:
435+
DumpFun("after global_reg_alloc", fun)
436+
437+
438+
def RegAllocLocal(unit, opt_stats, fout, verbose=False):
439+
for fun in unit.funs:
440+
PhaseFinalizeStackAndLocalRegAlloc(fun, opt_stats, fout)
441+
if verbose:
442+
DumpFun("after stack finalization", fun)

0 commit comments

Comments
 (0)