Skip to content

Commit

Permalink
IKVM v8.6.3.0 preoptimizer update: more aggresive optimizations + bra…
Browse files Browse the repository at this point in the history
…nch optimization
  • Loading branch information
jessiepathfinder committed Dec 28, 2019
1 parent 1fb5e6e commit 94f3fff
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CommonAssemblyInfo.cs.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using System.Reflection;
[assembly: AssemblyCopyright("Copyright (C) 2019 Jessie Lesbian (based on work by Jeroen Frijters and Windward Studios)")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("8.6.2.0")]
[assembly: AssemblyVersion("8.6.3.0")]

#if SIGNCODE
#pragma warning disable 1699
Expand Down
Binary file modified openjdk/commonAttributes.class
Binary file not shown.
Binary file modified openjdk/java/lang/PropertyConstants.class
Binary file not shown.
2 changes: 1 addition & 1 deletion runtime/compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ internal static void Compile(DynamicTypeWrapper.FinishContext context, TypeWrapp
c.Compile(b, 0);
b.Leave();
}
nonleaf = c.nonleaf;
nonleaf = true;
}
finally
{
Expand Down
198 changes: 148 additions & 50 deletions runtime/jessielesbian.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static bool experimentalOptimizations
{
get
{

return (optpasses > 0);
}
}
Expand All @@ -35,6 +36,85 @@ private static Instruction GetInstuction(NormalizedByteCode bc, int arg1, short
instruction.PatchOpCode(bc, arg1, arg2);
return instruction;
}
private static bool IsBranchInstruction(Instruction instruction)
{
switch (instruction.NormalizedOpCode)
{
case NormalizedByteCode.__goto:
case NormalizedByteCode.__goto_finally:
case NormalizedByteCode.__ifeq:
case NormalizedByteCode.__ifge:
case NormalizedByteCode.__ifgt:
case NormalizedByteCode.__ifle:
case NormalizedByteCode.__iflt:
case NormalizedByteCode.__ifne:
case NormalizedByteCode.__ifnonnull:
case NormalizedByteCode.__ifnull:
case NormalizedByteCode.__if_acmpeq:
case NormalizedByteCode.__if_acmpne:
case NormalizedByteCode.__if_icmpeq:
case NormalizedByteCode.__if_icmpge:
case NormalizedByteCode.__if_icmpgt:
case NormalizedByteCode.__if_icmple:
case NormalizedByteCode.__if_icmplt:
case NormalizedByteCode.__if_icmpne:
return true;
default:
return false;
}
}
private static bool IsLoadInstruction(Instruction instruction)
{
switch (instruction.NormalizedOpCode)
{
case NormalizedByteCode.__aload:
case NormalizedByteCode.__iload:
case NormalizedByteCode.__lload:
case NormalizedByteCode.__dload:
case NormalizedByteCode.__fload:
case NormalizedByteCode.__aconst_null:
case NormalizedByteCode.__iconst:
case NormalizedByteCode.__lconst_0:
case NormalizedByteCode.__lconst_1:
case NormalizedByteCode.__dconst_0:
case NormalizedByteCode.__dconst_1:
case NormalizedByteCode.__fconst_0:
case NormalizedByteCode.__fconst_1:
case NormalizedByteCode.__fconst_2:
case NormalizedByteCode.__ldc:
return true;
default:
return false;
}
}
private static bool IsArrayLoadInstruction(Instruction instruction)
{
switch (instruction.NormalizedOpCode)
{
case NormalizedByteCode.__aaload:
case NormalizedByteCode.__iaload:
case NormalizedByteCode.__laload:
case NormalizedByteCode.__daload:
case NormalizedByteCode.__faload:
return true;
default:
return false;
}
}
private static bool IsStoreInstruction(Instruction instruction)
{
switch (instruction.NormalizedOpCode)
{
case NormalizedByteCode.__astore:
case NormalizedByteCode.__istore:
case NormalizedByteCode.__lstore:
case NormalizedByteCode.__dstore:
case NormalizedByteCode.__fstore:
return true;
default:
return false;
}
}
internal static Instruction[] Optimize(Instruction[] instructions)
{
//Jessie Lesbian's IKVM.NET JIT Optimizer
Expand All @@ -45,37 +125,25 @@ internal static Instruction[] Optimize(Instruction[] instructions)
List<int> brtargets = new List<int>(instructions.Length);
for (int i = 1; i < instructions.Length; i++)
{
switch (instructions[i].NormalizedOpCode)
if (IsBranchInstruction(instructions[i]))
{
case NormalizedByteCode.__goto:
case NormalizedByteCode.__goto_finally:
case NormalizedByteCode.__ifeq:
case NormalizedByteCode.__ifge:
case NormalizedByteCode.__ifgt:
case NormalizedByteCode.__ifle:
case NormalizedByteCode.__iflt:
case NormalizedByteCode.__ifne:
case NormalizedByteCode.__ifnonnull:
case NormalizedByteCode.__ifnull:
case NormalizedByteCode.__if_acmpeq:
case NormalizedByteCode.__if_acmpne:
case NormalizedByteCode.__if_icmpeq:
case NormalizedByteCode.__if_icmpge:
case NormalizedByteCode.__if_icmpgt:
case NormalizedByteCode.__if_icmple:
case NormalizedByteCode.__if_icmplt:
case NormalizedByteCode.__if_icmpne:
brtargets.Add(instructions[i].TargetIndex);
break;
default:
continue;
brtargets.Add(i);
}
}
while (optimizations != 0)
{
optimizations = 0;
for (int i = 1; i < instructions.Length; i++)
{
int prevIndex = i - 1;
while (prevIndex > 0 || instructions[prevIndex].NormalizedOpCode == NormalizedByteCode.__nop)
{
prevIndex--;
}
if (brtargets.Contains(prevIndex) || IsBranchInstruction(instructions[prevIndex]))
{
continue;
}
Instruction current = instructions[i];
Instruction prev = instructions[i - 1];
if (brtargets.Contains(i))
Expand All @@ -90,7 +158,7 @@ internal static Instruction[] Optimize(Instruction[] instructions)
{
continue;
}
//peephole optimization: removal of unused arithmethc operations
//peephole optimization: anihilation of pops
if (current.NormalizedOpCode == NormalizedByteCode.__pop)
{
switch (prev.NormalizedOpCode)
Expand Down Expand Up @@ -135,20 +203,21 @@ internal static Instruction[] Optimize(Instruction[] instructions)
prev = GetInstuction(NormalizedByteCode.__pop);
optimizations = optimizations + 1;
break;
case NormalizedByteCode.__dup:
prev = GetInstuction(NormalizedByteCode.__nop);
current = GetInstuction(NormalizedByteCode.__nop);
optimizations = optimizations + 1;
break;
case NormalizedByteCode.__aload:
case NormalizedByteCode.__anewarray:
prev = GetInstuction(NormalizedByteCode.__nop);
current = GetInstuction(NormalizedByteCode.__nop);
break;
case NormalizedByteCode.__aaload:
prev = GetInstuction(NormalizedByteCode.__pop);
current = GetInstuction(NormalizedByteCode.__pop);
break;
default:
if (IsBranchInstruction(prev))
{
prev = GetInstuction(NormalizedByteCode.__nop);
current = GetInstuction(NormalizedByteCode.__nop);
optimizations = optimizations + 1;
}
if (IsArrayLoadInstruction(prev))
{
prev = GetInstuction(NormalizedByteCode.__pop);
optimizations = optimizations + 1;
}
break;
}
}
Expand All @@ -170,10 +239,21 @@ internal static Instruction[] Optimize(Instruction[] instructions)
break;
}
}
//peephole optimization: strength reduction
if (current.NormalizedOpCode == prev.NormalizedOpCode && IsBranchInstruction(current) && current.NormalizedArg1 == prev.NormalizedArg1)
{
current = GetInstuction(NormalizedByteCode.__dup);
optimizations = optimizations + 1;
}
if (current.NormalizedOpCode == prev.NormalizedOpCode && IsStoreInstruction(current) && current.NormalizedArg1 == prev.NormalizedArg1)
{
prev = GetInstuction(NormalizedByteCode.__pop);
optimizations = optimizations + 1;
}
//peephole optimization: optimize addition and subtraction
if((prev.NormalizedOpCode == NormalizedByteCode.__ineg))
if ((prev.NormalizedOpCode == NormalizedByteCode.__ineg))
{
if(current.NormalizedOpCode == NormalizedByteCode.__iadd)
if (current.NormalizedOpCode == NormalizedByteCode.__iadd)
{
current = GetInstuction(NormalizedByteCode.__isub);
prev = GetInstuction(NormalizedByteCode.__nop);
Expand Down Expand Up @@ -232,7 +312,7 @@ internal static Instruction[] Optimize(Instruction[] instructions)
}
}
//peephole optimization: remove unnecessary swaps
if(prev.NormalizedOpCode == NormalizedByteCode.__swap)
if (prev.NormalizedOpCode == NormalizedByteCode.__swap)
{
switch (current.NormalizedOpCode)
{
Expand All @@ -256,17 +336,6 @@ internal static Instruction[] Optimize(Instruction[] instructions)
current = GetInstuction(NormalizedByteCode.__nop);
optimizations = optimizations + 1;
}
//peephole optimization: local variable acession optimization
if (current.NormalizedOpCode == prev.NormalizedOpCode && current.NormalizedOpCode == NormalizedByteCode.__aload && current.NormalizedArg1 == prev.NormalizedArg1)
{
current = GetInstuction(NormalizedByteCode.__dup);
optimizations = optimizations + 1;
}
if (current.NormalizedOpCode == prev.NormalizedOpCode && current.NormalizedOpCode == NormalizedByteCode.__astore && current.NormalizedArg1 == prev.NormalizedArg1)
{
prev = GetInstuction(NormalizedByteCode.__pop);
optimizations = optimizations + 1;
}
//peephole optimization: return optimization
if (current.NormalizedOpCode == NormalizedByteCode.__return)
{
Expand All @@ -284,7 +353,36 @@ internal static Instruction[] Optimize(Instruction[] instructions)
break;
}
}
//sorry, I can't implement branch optimizations.
//branch optimizations
if(prev.NormalizedOpCode == NormalizedByteCode.__aconst_null)
{
if(current.NormalizedOpCode == NormalizedByteCode.__ifnull)
{
prev = GetInstuction(NormalizedByteCode.__nop);
current.PatchOpCode(NormalizedByteCode.__goto);
optimizations = optimizations + 1;
}
else if (current.NormalizedOpCode == NormalizedByteCode.__ifnonnull)
{
prev = GetInstuction(NormalizedByteCode.__nop);
current = prev;
optimizations = optimizations + 1;
}
}
if(IsBranchInstruction(current))
{
Instruction target = instructions[current.TargetIndex];
if (target.NormalizedOpCode == NormalizedByteCode.__goto)
{
current.TargetIndex = target.TargetIndex;
optimizations = optimizations + 1;
}
else if(target.NormalizedOpCode == NormalizedByteCode.__nop)
{
current.TargetIndex++;
optimizations = optimizations + 1;
}
}
instructions[i] = current;
instructions[i - 1] = prev;
}
Expand Down

0 comments on commit 94f3fff

Please sign in to comment.