From d2851434f0613bfa9971b4f2ecd1e5ae5601731e Mon Sep 17 00:00:00 2001 From: ike709 Date: Fri, 25 Oct 2024 15:01:20 -0500 Subject: [PATCH 1/5] String concat const folding --- DMCompiler/DM/Builders/DMASTFolder.cs | 7 ----- DMCompiler/Optimizer/AnnotatedBytecode.cs | 4 +++ DMCompiler/Optimizer/PeepholeOptimizations.cs | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/DMCompiler/DM/Builders/DMASTFolder.cs b/DMCompiler/DM/Builders/DMASTFolder.cs index ef41916191..c117824b65 100644 --- a/DMCompiler/DM/Builders/DMASTFolder.cs +++ b/DMCompiler/DM/Builders/DMASTFolder.cs @@ -254,13 +254,6 @@ private DMASTExpression FoldExpression(DMASTExpression? expression) { break; } - case DMASTAdd add: { - DMASTConstantString? lhsString = add.LHS as DMASTConstantString; - DMASTConstantString? rhsString = add.RHS as DMASTConstantString; - if (lhsString != null && rhsString != null) return new DMASTConstantString(expression.Location, lhsString.Value + rhsString.Value); - - break; - } #endregion Math diff --git a/DMCompiler/Optimizer/AnnotatedBytecode.cs b/DMCompiler/Optimizer/AnnotatedBytecode.cs index b84a212c93..f931b84b72 100644 --- a/DMCompiler/Optimizer/AnnotatedBytecode.cs +++ b/DMCompiler/Optimizer/AnnotatedBytecode.cs @@ -249,6 +249,10 @@ public void SetLocation(Location loc) { public Location GetLocation() { return Location; } + + public string ResolveString() { + return DMObjectTree.StringTable[Id]; + } } internal sealed class AnnotatedBytecodeArgumentType : IAnnotatedBytecode { diff --git a/DMCompiler/Optimizer/PeepholeOptimizations.cs b/DMCompiler/Optimizer/PeepholeOptimizations.cs index ad82b829a4..ef8b6592e2 100644 --- a/DMCompiler/Optimizer/PeepholeOptimizations.cs +++ b/DMCompiler/Optimizer/PeepholeOptimizations.cs @@ -1,4 +1,5 @@ using DMCompiler.Bytecode; +using DMCompiler.DM; namespace DMCompiler.Optimizer; @@ -678,6 +679,33 @@ public void Apply(List input, int index) { } } +// PushString [constant] +// PushString [constant] +// Add +// -> PushString [result] +internal sealed class ConstFoldAddStrings : IPeepholeOptimization { + public ReadOnlySpan GetOpcodes() { + return [ + DreamProcOpcode.PushString, + DreamProcOpcode.PushString, + DreamProcOpcode.Add, + ]; + } + + public void Apply(List input, int index) { + var firstInstruction = (AnnotatedBytecodeInstruction)input[index]; + var firstString = firstInstruction.GetArg(0); + var secondString = ((AnnotatedBytecodeInstruction)input[index+1]).GetArg(0); + + var combinedId = DMObjectTree.AddString(firstString.ResolveString() + secondString.ResolveString()); + + var args = new List(1) {new AnnotatedBytecodeString(combinedId, firstInstruction.Location)}; + + IPeepholeOptimization.ReplaceInstructions(input, index, 3, + new AnnotatedBytecodeInstruction(DreamProcOpcode.PushString, 1, args)); + } +} + // PushFloat [constant] // PushFloat [constant] // Subtract From 5bc9cef7647100f291e8ce7f8f00dfd00dccecde Mon Sep 17 00:00:00 2001 From: ike709 Date: Fri, 15 Nov 2024 19:08:19 -0600 Subject: [PATCH 2/5] Update DMCompiler/Optimizer/PeepholeOptimizations.cs --- DMCompiler/Optimizer/PeepholeOptimizations.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/Optimizer/PeepholeOptimizations.cs b/DMCompiler/Optimizer/PeepholeOptimizations.cs index c8c0022931..4763ef4912 100644 --- a/DMCompiler/Optimizer/PeepholeOptimizations.cs +++ b/DMCompiler/Optimizer/PeepholeOptimizations.cs @@ -786,7 +786,7 @@ public void Apply(List input, int index) { var firstString = firstInstruction.GetArg(0); var secondString = ((AnnotatedBytecodeInstruction)input[index+1]).GetArg(0); - var combinedId = DMObjectTree.AddString(firstString.ResolveString() + secondString.ResolveString()); + var combinedId = DMObjectTree.AddString(firstString.ResolveString() + secondString.ResolveString()); // TODO: Currently doesn't handle removing strings from the string tree that have no other references var args = new List(1) {new AnnotatedBytecodeString(combinedId, firstInstruction.Location)}; From a87abb8486f29a769b90383a57a907b9dad159cd Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 16 Nov 2024 09:17:37 -0600 Subject: [PATCH 3/5] fix merge --- DMCompiler/Optimizer/PeepholeOptimizations.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/Optimizer/PeepholeOptimizations.cs b/DMCompiler/Optimizer/PeepholeOptimizations.cs index 4763ef4912..684ac3c73f 100644 --- a/DMCompiler/Optimizer/PeepholeOptimizations.cs +++ b/DMCompiler/Optimizer/PeepholeOptimizations.cs @@ -781,7 +781,7 @@ public ReadOnlySpan GetOpcodes() { ]; } - public void Apply(List input, int index) { + public void Apply(DMCompiler compiler, List input, int index) { var firstInstruction = (AnnotatedBytecodeInstruction)input[index]; var firstString = firstInstruction.GetArg(0); var secondString = ((AnnotatedBytecodeInstruction)input[index+1]).GetArg(0); From 2bf720e46142b4284afedfc3f80c4c0cf5e06b43 Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 16 Nov 2024 09:36:59 -0600 Subject: [PATCH 4/5] fix it for realsies --- DMCompiler/Optimizer/AnnotatedBytecode.cs | 4 ++-- DMCompiler/Optimizer/PeepholeOptimizations.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DMCompiler/Optimizer/AnnotatedBytecode.cs b/DMCompiler/Optimizer/AnnotatedBytecode.cs index 14f8c81d5c..3bb38adb09 100644 --- a/DMCompiler/Optimizer/AnnotatedBytecode.cs +++ b/DMCompiler/Optimizer/AnnotatedBytecode.cs @@ -236,8 +236,8 @@ public Location GetLocation() { return Location; } - public string ResolveString() { - return DMObjectTree.StringTable[Id]; + public string ResolveString(DMCompiler compiler) { + return compiler.DMObjectTree.StringTable[Id]; } } diff --git a/DMCompiler/Optimizer/PeepholeOptimizations.cs b/DMCompiler/Optimizer/PeepholeOptimizations.cs index 078c7300ee..4c73e6926c 100644 --- a/DMCompiler/Optimizer/PeepholeOptimizations.cs +++ b/DMCompiler/Optimizer/PeepholeOptimizations.cs @@ -786,7 +786,7 @@ public void Apply(DMCompiler compiler, List input, int index var firstString = firstInstruction.GetArg(0); var secondString = ((AnnotatedBytecodeInstruction)input[index+1]).GetArg(0); - var combinedId = DMObjectTree.AddString(firstString.ResolveString() + secondString.ResolveString()); // TODO: Currently doesn't handle removing strings from the string tree that have no other references + var combinedId = compiler.DMObjectTree.AddString(firstString.ResolveString(compiler) + secondString.ResolveString(compiler)); // TODO: Currently doesn't handle removing strings from the string tree that have no other references var args = new List(1) {new AnnotatedBytecodeString(combinedId, firstInstruction.Location)}; From eef05aa9d7f7f27a91b10515b3b7da03563fc7d6 Mon Sep 17 00:00:00 2001 From: wixoa Date: Sat, 16 Nov 2024 21:14:14 -0500 Subject: [PATCH 5/5] Update DMCompiler/Optimizer/PeepholeOptimizations.cs --- DMCompiler/Optimizer/PeepholeOptimizations.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/DMCompiler/Optimizer/PeepholeOptimizations.cs b/DMCompiler/Optimizer/PeepholeOptimizations.cs index 4c73e6926c..a80d48f9d3 100644 --- a/DMCompiler/Optimizer/PeepholeOptimizations.cs +++ b/DMCompiler/Optimizer/PeepholeOptimizations.cs @@ -1,5 +1,4 @@ using DMCompiler.Bytecode; -using DMCompiler.DM; namespace DMCompiler.Optimizer;