2019-04-26 06:55:12 +02:00
|
|
|
using ChocolArm64.IntermediateRepresentation;
|
|
|
|
using ChocolArm64.State;
|
2018-10-31 02:43:02 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Translation
|
|
|
|
{
|
2019-02-28 03:03:31 +01:00
|
|
|
class RegisterUsage
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
private const long CallerSavedIntRegistersMask = 0x7fL << 9;
|
|
|
|
private const long PStateNzcvFlagsMask = 0xfL << 60;
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
private const long CallerSavedVecRegistersMask = 0xffffL << 16;
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
private RegisterMask[] _inputs;
|
|
|
|
private RegisterMask[] _outputs;
|
|
|
|
|
|
|
|
public RegisterUsage(BasicBlock entryBlock, int blocksCount)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
_inputs = new RegisterMask[blocksCount];
|
|
|
|
_outputs = new RegisterMask[blocksCount];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
HashSet<BasicBlock> visited = new HashSet<BasicBlock>();
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
Stack<BasicBlock> blockStack = new Stack<BasicBlock>();
|
|
|
|
|
|
|
|
List<BasicBlock> postOrderBlocks = new List<BasicBlock>(blocksCount);
|
|
|
|
|
|
|
|
visited.Add(entryBlock);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
blockStack.Push(entryBlock);
|
|
|
|
|
|
|
|
while (blockStack.TryPop(out BasicBlock block))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
if (block.Next != null && visited.Add(block.Next))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
blockStack.Push(block);
|
|
|
|
blockStack.Push(block.Next);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
2019-04-26 06:55:12 +02:00
|
|
|
else if (block.Branch != null && visited.Add(block.Branch))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
blockStack.Push(block);
|
|
|
|
blockStack.Push(block.Branch);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
2019-04-26 06:55:12 +02:00
|
|
|
else
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
postOrderBlocks.Add(block);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
RegisterMask[] cmnOutputMasks = new RegisterMask[blocksCount];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
bool modified;
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
bool firstPass = true;
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
do
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
modified = false;
|
2018-12-11 01:58:52 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
for (int blkIndex = postOrderBlocks.Count - 1; blkIndex >= 0; blkIndex--)
|
2018-12-11 01:58:52 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
BasicBlock block = postOrderBlocks[blkIndex];
|
2018-12-11 01:58:52 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (block.Predecessors.Count != 0 && !block.HasStateLoad)
|
|
|
|
{
|
|
|
|
BasicBlock predecessor = block.Predecessors[0];
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
RegisterMask cmnOutputs = predecessor.RegOutputs | cmnOutputMasks[predecessor.Index];
|
2018-12-11 01:58:52 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
RegisterMask outputs = _outputs[predecessor.Index];
|
2018-12-11 01:58:52 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
for (int pIndex = 1; pIndex < block.Predecessors.Count; pIndex++)
|
|
|
|
{
|
|
|
|
predecessor = block.Predecessors[pIndex];
|
2018-12-11 01:58:52 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
cmnOutputs &= predecessor.RegOutputs | cmnOutputMasks[predecessor.Index];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
outputs |= _outputs[predecessor.Index];
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
_inputs[block.Index] |= outputs & ~cmnOutputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (!firstPass)
|
|
|
|
{
|
|
|
|
cmnOutputs &= cmnOutputMasks[block.Index];
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (Exchange(cmnOutputMasks, block.Index, cmnOutputs))
|
|
|
|
{
|
|
|
|
modified = true;
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
outputs |= block.RegOutputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (Exchange(_outputs, block.Index, _outputs[block.Index] | outputs))
|
|
|
|
{
|
|
|
|
modified = true;
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
2019-04-26 06:55:12 +02:00
|
|
|
else if (Exchange(_outputs, block.Index, block.RegOutputs))
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
modified = true;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
firstPass = false;
|
|
|
|
}
|
|
|
|
while (modified);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
modified = false;
|
|
|
|
|
|
|
|
for (int blkIndex = 0; blkIndex < postOrderBlocks.Count; blkIndex++)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
BasicBlock block = postOrderBlocks[blkIndex];
|
|
|
|
|
|
|
|
RegisterMask inputs = block.RegInputs;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (block.Next != null)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
inputs |= _inputs[block.Next.Index];
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
2019-04-26 06:55:12 +02:00
|
|
|
|
|
|
|
if (block.Branch != null)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
inputs |= _inputs[block.Branch.Index];
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
inputs &= ~cmnOutputMasks[block.Index];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
if (Exchange(_inputs, block.Index, _inputs[block.Index] | inputs))
|
|
|
|
{
|
|
|
|
modified = true;
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 06:55:12 +02:00
|
|
|
while (modified);
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
private static bool Exchange(RegisterMask[] masks, int blkIndex, RegisterMask value)
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
2019-04-26 06:55:12 +02:00
|
|
|
RegisterMask oldValue = masks[blkIndex];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
masks[blkIndex] = value;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
return oldValue != value;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
public RegisterMask GetInputs(BasicBlock entryBlock) => _inputs[entryBlock.Index];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
public RegisterMask GetOutputs(BasicBlock block) => _outputs[block.Index];
|
2019-02-28 03:03:31 +01:00
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
public static long ClearCallerSavedIntRegs(long mask, ExecutionMode mode)
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
|
|
|
//TODO: ARM32 support.
|
2019-04-26 06:55:12 +02:00
|
|
|
if (mode == ExecutionMode.Aarch64)
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
|
|
|
mask &= ~(CallerSavedIntRegistersMask | PStateNzcvFlagsMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:55:12 +02:00
|
|
|
public static long ClearCallerSavedVecRegs(long mask, ExecutionMode mode)
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
|
|
|
//TODO: ARM32 support.
|
2019-04-26 06:55:12 +02:00
|
|
|
if (mode == ExecutionMode.Aarch64)
|
2019-02-28 03:03:31 +01:00
|
|
|
{
|
|
|
|
mask &= ~CallerSavedVecRegistersMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|