Skip to content

Commit be1f777

Browse files
committed
Remove unused labels in O2
This update tried hard to remove unused labels. Specially temporary ones. User labels are left untouched. If a several labels point to the same memory address only the last one prevails, except when it's an user label. If the last label is a temporary label and any other is an user label, the latter will prevail.
1 parent 7f4423c commit be1f777

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/arch/zx48k/backend/__init__.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,8 +2312,24 @@ def emit_end():
23122312

23132313

23142314
def remove_unused_labels(output: List[str]):
2315-
labels_used: Set[str] = set()
2315+
labels_used: Dict[str, List[int]] = defaultdict(list)
23162316
labels_to_delete: Dict[str, int] = {}
2317+
labels: Set[str] = set()
2318+
label_alias: Dict[str, str] = {}
2319+
2320+
prev = None
2321+
for i, ins in enumerate(output):
2322+
if ins and ins[-1] == ':':
2323+
ins = ins[:-1]
2324+
labels.add(ins)
2325+
if prev is not None:
2326+
if prev not in TMP_LABELS and ins in TMP_LABELS or prev in label_alias:
2327+
label_alias[ins] = prev
2328+
else:
2329+
label_alias[prev] = ins
2330+
prev = ins
2331+
else:
2332+
prev = None
23172333

23182334
for i, ins in enumerate(output):
23192335
try_label = ins[:-1]
@@ -2325,9 +2341,16 @@ def remove_unused_labels(output: List[str]):
23252341
continue
23262342

23272343
for op in Asm.opers(ins):
2328-
if op in TMP_LABELS:
2329-
labels_used.add(op)
2330-
labels_to_delete.pop(op, None)
2344+
if op in labels:
2345+
new_label = op
2346+
while new_label in label_alias:
2347+
new_label = label_alias[new_label]
2348+
2349+
labels_used[new_label].append(i)
2350+
labels_to_delete.pop(new_label, None)
2351+
2352+
if new_label != op:
2353+
output[i] = re.sub(r'\b' + op + r'\b', new_label, ins)
23312354

23322355
for i in sorted(labels_to_delete.values(), reverse=True):
23332356
output.pop(i)

src/arch/zx48k/optimizer/common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# -*- config: utf-8 -*-
22

3+
from typing import Dict
4+
5+
from . import labelinfo
6+
37
# counter for generating unique random fake values
48
RAND_COUNT = 0
59

610
# Labels which must start a basic block, because they're used in a JP/CALL
7-
LABELS = {} # Label -> LabelInfo object
11+
LABELS: Dict[str, labelinfo.LabelInfo] = {} # Label -> LabelInfo object
812

913
JUMP_LABELS = set([])
1014
MEMORY = [] # Instructions emitted by the backend

src/arch/zx48k/optimizer/memcell.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MemCell:
2424

2525
def __init__(self, instr: str, addr: int):
2626
self.addr = addr
27-
self.asm = instr # type: ignore
27+
self.asm = instr
2828

2929
@property
3030
def asm(self) -> Asm:
@@ -365,15 +365,4 @@ def replace_label(self, old_label: str, new_label: str):
365365
if old_label == new_label:
366366
return
367367

368-
tmp = re.compile(r'\b' + old_label + r'\b')
369-
last = 0
370-
l = len(new_label)
371-
372-
while True:
373-
match = tmp.search(self.inst)
374-
if not match:
375-
break
376-
377-
txt = self.inst
378-
self.asm = txt[:last + match.start()] + new_label + txt[last + match.end():] # type: ignore
379-
last += match.start() + l
368+
self.asm = re.sub(r'\b' + old_label + r'\b', new_label, self.code)

0 commit comments

Comments
 (0)