Skip to content

Commit 083176d

Browse files
ike709ike709
andauthored
Bitwise const folding (#2071)
Co-authored-by: ike709 <[email protected]>
1 parent 40e7458 commit 083176d

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

DMCompiler/DM/Builders/DMASTFolder.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,6 @@ private DMASTExpression FoldExpression(DMASTExpression? expression) {
219219

220220
break;
221221
}
222-
case DMASTBinaryAnd binaryAnd: {
223-
if (binaryAnd is { LHS: DMASTConstantInteger lhsInt, RHS: DMASTConstantInteger rhsInt }) {
224-
return new DMASTConstantInteger(expression.Location, lhsInt.Value & rhsInt.Value);
225-
}
226-
227-
break;
228-
}
229-
case DMASTBinaryOr binaryOr: {
230-
if (binaryOr is { LHS: DMASTConstantInteger lhsInt, RHS: DMASTConstantInteger rhsInt }) {
231-
return new DMASTConstantInteger(expression.Location, lhsInt.Value | rhsInt.Value);
232-
}
233-
234-
break;
235-
}
236-
case DMASTBinaryNot binaryNot: {
237-
if (binaryNot.Value is DMASTConstantInteger exprInt) {
238-
return new DMASTConstantInteger(expression.Location, (~exprInt.Value) & 0xFFFFFF);
239-
}
240-
241-
break;
242-
}
243222
case DMASTAdd add: {
244223
DMASTConstantString? lhsString = add.LHS as DMASTConstantString;
245224
DMASTConstantString? rhsString = add.RHS as DMASTConstantString;

DMCompiler/Optimizer/PeepholeOptimizations.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,78 @@ public void Apply(List<IAnnotatedBytecode> input, int index) {
618618
}
619619

620620
#region Constant Folding
621+
622+
// PushFloat [constant]
623+
// BitNot
624+
// -> PushFloat [result]
625+
internal sealed class ConstFoldBitNot : IPeepholeOptimization {
626+
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
627+
return [
628+
DreamProcOpcode.PushFloat,
629+
DreamProcOpcode.BitNot
630+
];
631+
}
632+
633+
public void Apply(List<IAnnotatedBytecode> input, int index) {
634+
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal1);
635+
636+
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(((~(int)pushVal1) & 0xFFFFFF), firstInstruction.Location)};
637+
638+
IPeepholeOptimization.ReplaceInstructions(input, index, 2,
639+
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
640+
}
641+
}
642+
643+
// PushFloat [constant]
644+
// PushFloat [constant]
645+
// BitOr
646+
// -> PushFloat [result]
647+
internal sealed class ConstFoldBitOr : IPeepholeOptimization {
648+
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
649+
return [
650+
DreamProcOpcode.PushFloat,
651+
DreamProcOpcode.PushFloat,
652+
DreamProcOpcode.BitOr,
653+
];
654+
}
655+
656+
public void Apply(List<IAnnotatedBytecode> input, int index) {
657+
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal1);
658+
659+
IPeepholeOptimization.GetInstructionAndValue(input[index + 1], out var pushVal2);
660+
661+
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(((int)pushVal1 | (int)pushVal2), firstInstruction.Location)};
662+
663+
IPeepholeOptimization.ReplaceInstructions(input, index, 3,
664+
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
665+
}
666+
}
667+
668+
// PushFloat [constant]
669+
// PushFloat [constant]
670+
// BitAnd
671+
// -> PushFloat [result]
672+
internal sealed class ConstFoldBitAnd : IPeepholeOptimization {
673+
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
674+
return [
675+
DreamProcOpcode.PushFloat,
676+
DreamProcOpcode.PushFloat,
677+
DreamProcOpcode.BitAnd,
678+
];
679+
}
680+
681+
public void Apply(List<IAnnotatedBytecode> input, int index) {
682+
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal1);
683+
684+
IPeepholeOptimization.GetInstructionAndValue(input[index + 1], out var pushVal2);
685+
686+
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeFloat(((int)pushVal1 & (int)pushVal2), firstInstruction.Location)};
687+
688+
IPeepholeOptimization.ReplaceInstructions(input, index, 3,
689+
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushFloat, 1, args));
690+
}
691+
}
692+
621693
// PushFloat [constant]
622694
// PushFloat [constant]
623695
// Multiply

0 commit comments

Comments
 (0)