Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional label optimization #2218

Merged
merged 5 commits into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DMCompiler/Optimizer/BytecodeOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ internal void Optimize(List<IAnnotatedBytecode> input) {
RemoveUnreferencedLabels(input);
JoinAndForwardLabels(input);
RemoveUnreferencedLabels(input);
RemoveImmediateJumps(input);
RemoveUnreferencedLabels(input);

_peepholeOptimizer.RunPeephole(input);

// Run label optimizations again due to possibly removed jumps in peephole optimizers
RemoveUnreferencedLabels(input);
JoinAndForwardLabels(input);
RemoveUnreferencedLabels(input);
RemoveImmediateJumps(input);
RemoveUnreferencedLabels(input);
}

private void RemoveUnreferencedLabels(List<IAnnotatedBytecode> input) {
Expand All @@ -40,6 +49,22 @@ private void RemoveUnreferencedLabels(List<IAnnotatedBytecode> input) {
}
}

/**
* <summary>Removes jumps for which the next element is the jump's destination</summary>
*/
private void RemoveImmediateJumps(List<IAnnotatedBytecode> input) {
for (int i = input.Count - 2; i >= 0; i--) {
if (input[i] is AnnotatedBytecodeInstruction { Opcode: Bytecode.DreamProcOpcode.Jump } instruction) {
if (input[i + 1] is AnnotatedBytecodeLabel followingLabel) {
AnnotatedBytecodeLabel jumpLabelName = instruction.GetArg<AnnotatedBytecodeLabel>(0);
if (jumpLabelName.LabelName == followingLabel.LabelName) {
input.RemoveAt(i);
}
}
}
}
}

private void JoinAndForwardLabels(List<IAnnotatedBytecode> input) {
Dictionary<string, string> labelAliases = new();
for (int i = 0; i < input.Count; i++) {
Expand Down
Loading